SharedStorageSelectURLOperation
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 SharedStorageSelectURLOperation của Shared Storage API đại diện cho thao tác URL Selection output gate.
Phương thức phiên bản
run()Đã lỗi thời-
Xác định cấu trúc mà phương thức
run()được định nghĩa bên trong thao tác URL Selection output gate phải tuân theo.
Ví dụ
Trong ví dụ này, một lớp tên SelectURLOperation được định nghĩa trong worklet và được đăng ký sử dụng SharedStorageWorkletGlobalScope.register() với tên ab-testing. SharedStorageSelectURLOperation xác định cấu trúc mà lớp này cần tuân theo.
// ab-testing-worklet.js
class SelectURLOperation {
async run(urls, data) {
// Đọc nhóm thực nghiệm của người dùng từ Shared Storage
const experimentGroup = await this.sharedStorage.get("ab-testing-group");
// Trả về số nhóm
return experimentGroup;
}
}
// Đăng ký thao tác
register("ab-testing", SelectURLOperation);
Note:
Có thể định nghĩa và đăng ký nhiều thao tác trong cùng script module shared storage worklet với các tên khác nhau; xem SharedStorageOperation để biết ví dụ.
Trong ngữ cảnh duyệt web chính, thao tác ab-testing được gọi sử dụng phương thức WindowSharedStorage.selectURL():
// Ngẫu nhiên gán người dùng vào nhóm 0 hoặc 1
function getExperimentGroup() {
return Math.round(Math.random());
}
async function injectContent() {
// Đăng ký Shared Storage worklet
await window.sharedStorage.worklet.addModule("ab-testing-worklet.js");
// Gán người dùng vào nhóm ngẫu nhiên (0 hoặc 1) và lưu trong Shared Storage
window.sharedStorage.set("ab-testing-group", getExperimentGroup(), {
ignoreIfPresent: true,
});
// Chạy thao tác URL selection
const fencedFrameConfig = await window.sharedStorage.selectURL(
"ab-testing",
[
{ url: `https://your-server.example/content/default-content.html` },
{ url: `https://your-server.example/content/experiment-content-a.html` },
],
{
resolveToConfig: true,
},
);
// Render URL đã chọn vào fenced frame
document.getElementById("content-slot").config = fencedFrameConfig;
}
injectContent();