WindowSharedStorage: worklet-Eigenschaft
Deprecated
Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.>
Die schreibgeschützte worklet-Eigenschaft der WindowSharedStorage-Schnittstelle enthält die SharedStorageWorklet-Instanz, die den gemeinsamen Speicher-Worklet für den aktuellen Ursprung darstellt.
SharedStorageWorklet enthält die Methode addModule(), die verwendet wird, um ein Modul zum gemeinsamen Speicher-Worklet hinzuzufügen.
Wert
Ein SharedStorageWorklet-Objekt.
Beispiele
js
// Randomly assigns a user to a group 0 or 1
function getExperimentGroup() {
return Math.round(Math.random());
}
async function injectContent() {
// Add the module to 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();
Schauen Sie sich die Shared Storage API-Startseite an für eine ausführliche Erklärung dieses Beispiels und Links zu weiteren Beispielen.