SharedStorageSelectURLOperation
SharedStorageSelectURLOperation 介面是 Shared Storage API 的一部分,代表一個 URL 選擇輸出門操作。
例項方法
run()實驗性-
定義了在 URL 選擇輸出門操作中定義的
run()方法應遵循的結構。
示例
在此示例中,一個名為 SelectURLOperation 的類在 worklet 中定義,並使用 SharedStorageWorkletGlobalScope.register() 以 ab-testing 的名稱註冊。SharedStorageSelectURLOperation 定義了這個類需要遵循的結構,本質上定義了 run() 方法所需的引數。除了這個要求之外,類的功能可以靈活定義。
js
// ab-testing-worklet.js
class SelectURLOperation {
async run(urls, data) {
// Read the user's experiment group from Shared Storage
const experimentGroup = await this.sharedStorage.get("ab-testing-group");
// Return the group number
return experimentGroup;
}
}
// Register the operation
register("ab-testing", SelectURLOperation);
注意: 可以在同一個共享儲存工作執行緒模組指令碼中定義和註冊多個具有不同名稱的操作;有關示例,請參閱SharedStorageOperation。
在主瀏覽上下文中,ab-testing 操作透過 WindowSharedStorage.selectURL() 方法呼叫。
js
// Randomly assigns a user to a group 0 or 1
function getExperimentGroup() {
return Math.round(Math.random());
}
async function injectContent() {
// Register the Shared Storage worklet
await window.sharedStorage.worklet.addModule("ab-testing-worklet.js");
// Assign user to a random group (0 or 1) and store it in Shared Storage
window.sharedStorage.set("ab-testing-group", getExperimentGroup(), {
ignoreIfPresent: true,
});
// Run the URL selection operation
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 the chosen URL into a fenced frame
document.getElementById("content-slot").config = fencedFrameConfig;
}
injectContent();
有關此示例的更多詳細資訊以及其他示例連結,請參閱 Shared Storage API 入門頁面。
規範
此特性似乎未在任何規範中定義。瀏覽器相容性
載入中…