WorkletSharedStorage
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Giao diện WorkletSharedStorage của Shared Storage API đại diện cho bộ nhớ chia sẻ cho một nguồn gốc cụ thể trong bối cảnh worklet.
WorkletSharedStorage được truy cập qua SharedStorageWorkletGlobalScope.sharedStorage.
Thuộc tính phiên bản
contextDeprecated Non-standard-
Chứa dữ liệu ngữ cảnh được truyền vào worklet bộ nhớ chia sẻ từ bối cảnh duyệt web liên kết qua phương thức
FencedFrameConfig.setSharedStorageContext().
Phương thức phiên bản
WorkletSharedStorage kế thừa các thuộc tính từ giao diện cha của nó, SharedStorage.
get()Deprecated-
Lấy một giá trị từ bộ nhớ chia sẻ.
length()Deprecated-
Trả về số lượng mục hiện đang được lưu trong bộ nhớ chia sẻ cho nguồn gốc hiện tại.
remainingBudget()Deprecated-
Trả về ngân sách điều hướng còn lại cho nguồn gốc hiện tại.
WorkletSharedStorage cũng bao gồm các phương thức sau vì nó có async iterator được định nghĩa trên nó:
entries()Deprecated-
Trả về một async iterator mới cho các cặp key-value của các thuộc tính có thể liệt kê của một phiên bản đối tượng
WorkletSharedStorage. keys()Deprecated-
Trả về một async iterator mới chứa các khóa cho mỗi mục trong một phiên bản đối tượng
WorkletSharedStorage. -
Trả về hàm
entries()theo mặc định.
Ví dụ
>Truyền dữ liệu ngữ cảnh qua setSharedStorageContext()
Bạn có thể sử dụng Private Aggregation API để tạo báo cáo kết hợp dữ liệu cấp sự kiện bên trong các fenced frame với dữ liệu ngữ cảnh từ tài liệu nhúng. setSharedStorageContext() có thể được dùng để truyền dữ liệu ngữ cảnh từ phần nhúng đến các worklet bộ nhớ chia sẻ được khởi tạo bởi Protected Audience API.
Trong ví dụ này, chúng ta lưu trữ dữ liệu từ cả trang nhúng và fenced frame bằng cách dùng shared storage.
Trên trang nhúng, chúng ta đặt ID sự kiện giả làm ngữ cảnh bộ nhớ chia sẻ bằng setSharedStorageContext():
const frameConfig = await navigator.runAdAuction({ resolveToConfig: true });
// Data from the embedder that you want to pass to the shared storage worklet
frameConfig.setSharedStorageContext("some-event-id");
const frame = document.createElement("fencedframe");
frame.config = frameConfig;
Trong fenced frame, sau khi thêm module worklet với window.sharedStorage.worklet.addModule(), chúng ta gửi dữ liệu cấp sự kiện vào module worklet bộ nhớ chia sẻ bằng window.sharedStorage.run() (điều này không liên quan đến dữ liệu ngữ cảnh từ tài liệu nhúng):
const frameData = {
// Data available only inside the fenced frame
};
await window.sharedStorage.worklet.addModule("reporting-worklet.js");
await window.sharedStorage.run("send-report", {
data: {
frameData,
},
});
Trong worklet reporting-worklet.js, chúng ta đọc ID sự kiện của tài liệu nhúng từ sharedStorage.context và dữ liệu cấp sự kiện của frame từ đối tượng data. Sau đó chúng ta báo cáo chúng qua Private Aggregation:
class ReportingOperation {
convertEventIdToBucket(eventId) {
// …
}
convertEventPayloadToValue(info) {
// …
}
async run(data) {
// Data from the embedder
const eventId = sharedStorage.context;
// Data from the fenced frame
const eventPayload = data.frameData;
privateAggregation.sendHistogramReport({
bucket: convertEventIdToBucket(eventId),
value: convertEventPayloadToValue(eventPayload),
});
}
}
register("send-report", ReportingOperation);