WorkletSharedStorage: get()-Methode
Deprecated
Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.>
Die get()-Methode der WorkletSharedStorage-Schnittstelle ruft einen Wert aus dem gemeinsamen Speicher ab.
Syntax
get(key)
Parameter
key-
Ein String, der den Schlüssel des Schlüssel-Wert-Paares darstellt, das Sie abrufen möchten.
Rückgabewert
Ein Promise, das entweder mit einem String erfüllt wird, der dem Wert des abgerufenen Schlüssel-Wert-Paares entspricht, oder undefined, wenn der angegebene key nicht im gemeinsamen Speicher gefunden wird.
Ausnahmen
TypeError-
Ausgelöst, wenn:
- Das Worklet-Modul noch nicht mit
addModule()hinzugefügt wurde. keydie vom Browser definierte maximale Länge überschreitet.- Die aufrufende Webseite die Shared Storage API nicht im Rahmen eines erfolgreichen Privacy Sandbox-Anmeldungsprozesses eingeschlossen hat.
- Das Worklet-Modul noch nicht mit
Beispiele
>Messen der K+ Frequenz
Das folgende Beispiel misst die K+ Frequenz von Inhaltsansichten. Mitunter als "effektive Frequenz" beschrieben, bezieht sich die K-Frequenz auf die Mindestanzahl von Ansichten, bevor ein Benutzer bestimmte Inhalte erkennt oder sich daran erinnert (oft im Kontext von Anzeigenansichten verwendet).
Das Hauptseitenskript:
// k-frequency-measurement.js
async function injectContent() {
// Load the Shared Storage worklet
await window.sharedStorage.worklet.addModule("k-freq-measurement-worklet.js");
// Run the K-frequency measurement operation
await window.sharedStorage.run("k-freq-measurement", {
data: { kFreq: 3, contentId: 123 },
});
}
injectContent();
Das Worklet-Modul wird unten gezeigt:
// k-frequency-measurement-worklet.js
// Scale factor for handling noise added to data
const SCALE_FACTOR = 65536;
/**
* The bucket key must be a number, and in this case, it is simply the content
* ID itself. For more complex bucket key construction, see other use cases in
* this demo.
*/
function convertContentIdToBucket(contentId) {
return BigInt(contentId);
}
class KFreqMeasurementOperation {
async run(data) {
const { kFreq, contentId } = data;
// Read from Shared Storage
const hasReportedContentKey = "has-reported-content";
const impressionCountKey = "impression-count";
const hasReportedContent =
(await this.sharedStorage.get(hasReportedContentKey)) === "true";
const impressionCount = parseInt(
(await this.sharedStorage.get(impressionCountKey)) || 0,
10,
);
// Do not report if a report has been sent already
if (hasReportedContent) {
return;
}
// Check impression count against frequency limit
if (impressionCount < kFreq) {
await this.sharedStorage.set(impressionCountKey, impressionCount + 1);
return;
}
// Generate the aggregation key and the aggregatable value
const bucket = convertContentIdToBucket(contentId);
const value = 1 * SCALE_FACTOR;
// Send an aggregatable report via the Private Aggregation API
privateAggregation.sendHistogramReport({ bucket, value });
// Set the report submission status flag
await this.sharedStorage.set(hasReportedContentKey, "true");
}
}
// Register the operation
register("k-freq-measurement", KFreqMeasurementOperation);
Für weitere Details zu diesem Beispiel siehe K+ frequency measurement. Siehe die Shared Storage API Startseite für Links zu weiteren Beispielen.
Spezifikationen
Diese Funktion scheint in keiner Spezifikation definiert zu sein.>Browser-Kompatibilität
Siehe auch
- Shared Storage API
- Noise and scaling auf privacysandbox.google.com (2023)