WindowSharedStorage
Deprecated
Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.>
Das WindowSharedStorage-Interface der Shared Storage API repräsentiert den gemeinsam genutzten Speicher für einen bestimmten Origin innerhalb eines Standard-Browsing-Kontexts.
Auf WindowSharedStorage wird über Window.sharedStorage zugegriffen.
Instanzeigenschaften
workletSchreibgeschützt-
Enthält die
SharedStorageWorklet-Instanz, die das Shared-Storage-Worklet für den aktuellen Origin repräsentiert.SharedStorageWorkletenthält die MethodeaddModule(), mit der dem Shared-Storage-Worklet ein Modul hinzugefügt wird.
Instanzmethoden
WindowSharedStorage erbt Eigenschaften von seinem übergeordneten Interface SharedStorage.
run()-
Führt eine Run-Output-Gate-Operation aus, die in einem Modul registriert wurde, das dem
SharedStorageWorkletdes aktuellen Origins hinzugefügt wurde. selectURL()-
Führt eine URL-Selection-Output-Gate-Operation aus, die in einem Modul registriert wurde, das dem
SharedStorageWorkletdes aktuellen Origins hinzugefügt wurde.
Beispiele
// 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();
Eine Erläuterung dieses Beispiels und Links zu weiteren Beispielen finden Sie auf der Übersichtsseite zur Shared Storage API.