Tiện ích mở rộng trình duyệt của bạn có thể cần phải hoạt động với các tệp để cung cấp đầy đủ chức năng của nó. Bài viết này xem xét năm cơ chế bạn có để xử lý tệp
- Tải tệp xuống thư mục tải xuống do người dùng chọn
- Mở tệp bằng bộ chọn tệp trên trang web
- Mở tệp bằng cách kéo và thả vào trang web
- Lưu trữ cục bộ tệp hoặc đốm màu bằng IndexedDB bằng thư viện lưu trữ tệp idb
- Truyền tệp tới ứng dụng gốc trên máy tính của người dùng
Đối với mỗi cơ chế trong số này, chúng tôi giới thiệu cách sử dụng chúng với các tham chiếu đến tài liệu, hướng dẫn API có liên quan và bất kỳ ví dụ nào cho biết cách sử dụng API
Tải xuống tệp bằng API tải xuống
Cơ chế này cho phép bạn lấy tệp từ trang web của mình [hoặc bất kỳ vị trí nào bạn có thể xác định là URL] đến máy tính của người dùng. Phương thức chính là
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
5, ở dạng đơn giản nhất chấp nhận một URL và tải tệp từ URL đó xuống thư mục tải xuống mặc định của người dùngbrowser.downloads.download[{url: "//example.org/image.png"}]
Bạn có thể cho phép người dùng tải xuống vị trí họ chọn bằng cách chỉ định tham số
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
6Ghi chú. Sử dụng URL. createObjectURL[], bạn cũng có thể tải xuống các tệp và đốm màu được xác định trong JavaScript của mình, có thể bao gồm nội dung cục bộ được truy xuất từ IndexedDB
API tải xuống cũng cung cấp các tính năng để hủy, tạm dừng, tiếp tục, xóa và xóa tải xuống;
Để sử dụng API này, bạn cần có quyền API
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
7 được chỉ định trong tệp async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
8 của bạnThí dụ. Tài liệu tham khảo API tải xuống mới nhất. API tải xuống
Mở tệp trong tiện ích mở rộng bằng bộ chọn tệp
Nếu bạn muốn làm việc với một tệp từ máy tính của người dùng, một tùy chọn là cho phép người dùng chọn một tệp bằng trình duyệt tệp của máy tính. Tạo một trang mới hoặc chèn mã vào một trang hiện có để sử dụng loại
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
9 của phần tử HTML async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
0 để cung cấp cho người dùng một bộ chọn tệp. Khi người dùng đã chọn một hoặc nhiều tệp, tập lệnh được liên kết với trang có thể truy cập nội dung của tệp bằng API tệp DOM, giống như cách ứng dụng web thực hiệnThí dụ. Hướng dẫn tưởng tượng. Sử dụng tệp từ tài liệu tham khảo API ứng dụng web. phần tử đầu vào HTML. API tệp DOM
Ghi chú. Nếu bạn muốn truy cập hoặc xử lý tất cả các tệp trong một thư mục đã chọn, bạn có thể làm như vậy bằng cách sử dụng
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
0 để chọn thư mục và trả lại tất cả các tệp trong đóMở tệp trong tiện ích mở rộng bằng cách kéo và thả
API kéo và thả trên web cung cấp giải pháp thay thế cho việc sử dụng bộ chọn tệp. Để sử dụng phương pháp này, hãy thiết lập 'vùng thả xuống' phù hợp với giao diện người dùng của bạn, sau đó thêm người nghe cho các sự kiện
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
1, async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
2 và async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
3 vào phần tử. Trong trình xử lý sự kiện drop, mã của bạn có thể truy cập bất kỳ tệp nào do người dùng drop từ đối tượng được cung cấp bởi thuộc tính async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
4 bằng cách sử dụng async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
5. Sau đó, mã của bạn có thể truy cập và thao tác với các tệp bằng API tệp DOMThí dụ. hướng dẫn tưởng tượng. Sử dụng tệp từ ứng dụng web. Tham chiếu API kéo và thả tệp. API tệp DOM
Lưu trữ dữ liệu tệp cục bộ bằng thư viện lưu trữ tệp IndexedDB
Nếu tiện ích mở rộng của bạn cần lưu tệp cục bộ, thư viện idb-file-storage sẽ cung cấp trình bao bọc dựa trên Promise đơn giản cho API IndexedDB để hỗ trợ lưu trữ và truy xuất tệp và đốm màu
Trên Firefox, thư viện này cũng cung cấp trình bao bọc API dựa trên Promise cho API
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
6 không chuẩn. [API async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
6 cho phép các tiện ích mở rộng tạo và duy trì đối tượng tệp cơ sở dữ liệu IndexedDB cung cấp API để đọc và thay đổi nội dung của tệp mà không cần tải tất cả tệp vào bộ nhớ. ]Các tính năng chính của thư viện là
getFileStorageTrả về một phiên bản
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
8, tạo bộ lưu trữ được đặt tên nếu nó không tồn tạiIDBFileStorageCung cấp các phương thức để lưu và truy xuất tệp, chẳng hạn như
- list để lấy danh sách tệp được lọc tùy chọn trong cơ sở dữ liệu
- put để thêm tệp hoặc đốm màu vào cơ sở dữ liệu
- get để truy xuất một tệp hoặc đốm màu từ cơ sở dữ liệu
- remove để xóa một tệp hoặc đốm màu khỏi cơ sở dữ liệu
Ví dụ Hình ảnh do Cửa hàng sưu tầm minh họa cách sử dụng hầu hết các tính năng này. [IDBMutableFile không được bao gồm, nhưng bạn có thể tìm thấy các ví dụ trong các ví dụ về lưu trữ tệp idb cùng với một số ví dụ khác về hoạt động của thư viện]
Ví dụ về Hình ảnh đã sưu tập trong Cửa hàng cho phép người dùng thêm hình ảnh vào bộ sưu tập bằng một tùy chọn trên menu ngữ cảnh hình ảnh. Hình ảnh đã chọn được thu thập trong cửa sổ bật lên và có thể được lưu vào bộ sưu tập được đặt tên. Nút trên thanh công cụ [
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
9] mở ra trang bộ sưu tập điều hướng, trên đó người dùng có thể xem và xóa các hình ảnh đã lưu, với tùy chọn bộ lọc để thu hẹp các lựa chọn. Xem ví dụ trong hành độngHoạt động của thư viện có thể được hiểu bằng cách xem image-store. js trong /utils/
Tạo cửa hàng và lưu hình ảnh
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
20 được gọi khi người dùng nhấp vào lưu trong cửa sổ bật lên và đã cung cấp tên cho bộ sưu tập hình ảnhĐầu tiên,
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
21 tạo, nếu nó chưa tồn tại hoặc truy xuất cơ sở dữ liệu IndexedDB async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
22 cho đối tượng async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
23. async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
24 sau đó thêm từng hình ảnh được thu thập vào cơ sở dữ liệu, bên dưới tên bộ sưu tập, sử dụng id duy nhất của blob [tên tệp]Nếu hình ảnh được lưu trữ có cùng tên với hình ảnh đã có trong cơ sở dữ liệu, nó sẽ bị ghi đè. Nếu bạn muốn tránh điều này, trước tiên hãy truy vấn cơ sở dữ liệu bằng cách sử dụng
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
25 với bộ lọc cho tên tệp; Truy xuất hình ảnh được lưu trữ để hiển thị
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
2async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
26 được gọi khi người dùng nhấp vào xem hoặc tải lại trong trang bộ sưu tập điều hướng. async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
27 mở cơ sở dữ liệu async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
22, sau đó async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
25 nhận danh sách đã lọc gồm các hình ảnh được lưu trữ. Danh sách này sau đó được sử dụng để truy xuất hình ảnh với async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
30 và tạo danh sách để quay lại giao diện người dùngLưu ý việc sử dụng
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
31 để tạo URL tham chiếu đốm hình ảnh. URL này sau đó được sử dụng trong giao diện người dùng [navigate-collection. jscollection. js] để hiển thị hình ảnhXóa hình ảnh đã thu thập
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
3async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
32 được gọi khi người dùng nhấp vào xóa trong trang bộ sưu tập điều hướng. Một lần nữa, async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
27 mở cơ sở dữ liệu async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
22 sau đó async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
35 xóa từng ảnh khỏi danh sách ảnh đã lọcLưu ý việc sử dụng
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
36 để thu hồi URL blob một cách rõ ràng. Điều này cho phép trình thu gom rác giải phóng bộ nhớ được phân bổ cho URL. Nếu điều này không được thực hiện, bộ nhớ sẽ không được trả lại cho đến khi trang mà nó được tạo được đóng lại. Nếu URL được tạo trong trang nền của tiện ích mở rộng, URL này sẽ không được tải cho đến khi tiện ích mở rộng bị vô hiệu hóa, gỡ cài đặt hoặc tải lại, do đó, việc giữ bộ nhớ này một cách không cần thiết có thể ảnh hưởng đến hiệu suất của trình duyệt. Nếu URL được tạo trong trang của tiện ích mở rộng [tab mới, cửa sổ bật lên hoặc thanh bên], bộ nhớ sẽ được giải phóng khi đóng trang, nhưng vẫn nên thu hồi URL khi không còn cần thiếtSau khi URL blob đã bị thu hồi, mọi nỗ lực tải nó sẽ dẫn đến lỗi. Ví dụ: nếu URL blob được sử dụng làm thuộc tính
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
37 của thẻ async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
38, hình ảnh sẽ không tải và sẽ không hiển thị. Do đó, cách tốt nhất là xóa mọi URL blob bị thu hồi khỏi các phần tử HTML được tạo khi URL blob bị thu hồiThí dụ. Lưu trữ tài liệu tham khảo API hình ảnh được thu thập. thư viện lưu trữ tệp idb
Ghi chú. Bạn cũng có thể sử dụng API Web IndexedDB đầy đủ để lưu trữ dữ liệu từ tiện ích mở rộng của mình. Điều này có thể hữu ích khi bạn cần lưu trữ dữ liệu không được xử lý tốt bằng các cặp khóa/giá trị đơn giản do API lưu trữ DOM cung cấp
Xử lý tệp trong một ứng dụng cục bộ
Khi bạn có ứng dụng gốc hoặc muốn cung cấp các tính năng gốc bổ sung để xử lý tệp, hãy sử dụng tính năng nhắn tin gốc để chuyển tệp đến ứng dụng gốc để xử lý
Bạn có hai lựa chọn
Nhắn tin dựa trên kết nốiTại đây, bạn kích hoạt quy trình với
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
39, trả về một đối tượng async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
50. Sau đó, bạn có thể chuyển một thông báo JSON tới ứng dụng gốc bằng hàm async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
51 của async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
52. Sử dụng chức năng async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
53 của async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
52, bạn có thể nghe tin nhắn từ ứng dụng gốc. Ứng dụng gốc được mở nếu nó không chạy khi async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
39 được gọi và ứng dụng vẫn chạy cho đến khi tiện ích mở rộng gọi async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
56 hoặc trang kết nối với nó bị đóngNhắn tin không kết nốiTại đây, bạn sử dụng
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
57 để gửi một thông báo JSON đến một phiên bản mới, tạm thời của ứng dụng gốc. Trình duyệt đóng ứng dụng gốc sau khi nhận được bất kỳ thông báo nào từ ứng dụng gốcĐể thêm tệp hoặc blob mà bạn muốn ứng dụng gốc xử lý, hãy sử dụng
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
58Để sử dụng phương pháp này, tiện ích mở rộng phải yêu cầu quyền
async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
59 hoặc quyền tùy chọn trong tệp async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
8 của nó. Khi sử dụng quyền tùy chọn, hãy nhớ kiểm tra xem quyền đó đã được cấp chưa và nếu cần, hãy yêu cầu quyền từ người dùng bằng API async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
61. Ngược lại, ứng dụng gốc phải cấp quyền cho tiện ích mở rộng bằng cách đưa ID của nó vào trường async function saveCollectedBlobs[collectionName, collectedBlobs] {
const storedImages = await getFileStorage[{name: "stored-images"}];
for [const item of collectedBlobs] {
await storedImages.put[`${collectionName}/${item.uuid}`, item.blob];
}
}
62 của tệp kê khai ứng dụngThí dụ. Nhắn tin gốc [chỉ minh họa cách nhắn tin đơn giản] Hướng dẫn. Tài liệu tham khảo API nhắn tin gốc. API thời gian chạy