/* Copyright 2023 Google LLC. SPDX-License-Identifier: Apache-2.0 */ const logEl = document.getElementById('log'); function log(message) { console.log(message); logEl.innerHTML += '

' + message + '<\p>'; } log(`HTML meta tag in this page includes a trial token`); const otMetaTag = document.querySelector('meta[http-equiv="origin-trial"]'); const token = otMetaTag.getAttribute('content'); const decodedToken = decodeToken(token) const expiryDate = new Date(decodedToken.payload.expiry * 1000); const feature = decodedToken.payload.feature; const isThirdParty = decodedToken.payload.isThirdParty; const tokenOrigin = decodedToken.payload.origin; // Check if the origin trial feature is available. // This will only work if the trial is current, and the token is valid and used correctly. // This demo currently uses a token for the WebSQL deprecation trial: https://developer.chrome.com/origintrials/#/view_trial/3977804370874990593 function isTrialFeatureEnabled() { return !! ('openDatabase' in window); } async function accessTrialFeature() { log(`Token feature: ${feature}`); log(`Token registered for origin: ${tokenOrigin}`); log(isThirdParty ? `Token is third-party` : `Token is not third-party`); log(`Token expiry date:
${expiryDate}`); log('/js/main.js is attempting to access the trial feature...'); isTrialFeatureEnabled() ? log(`Trial feature ${feature} is available 😺`) : log(`Trial feature ${feature} is not available 😿`); } try { accessTrialFeature(); } catch (error) { log('Error attempting to access trial feature:', error); } // Code adapted from https://github.com/Jxck/origin-trials-viewer/blob/main/util.js function base64decode(str) { return new Uint8Array([...atob(str)].map(a => a.charCodeAt(0))); } function decodeToken(token) { const buf = base64decode(token); const view = new DataView(buf.buffer) const version = view.getUint8() const signature = buf.slice(1, 65) const length = view.getUint32(65, false) const payload = JSON.parse((new TextDecoder()).decode(buf.slice(69, 69 + length))) return {payload, version, length, signature} }