Skip to content

Commit

Permalink
Serialize origin trial expiration dates (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
rviscomi authored Apr 16, 2024
1 parent abf1f48 commit 3968270
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 93 deletions.
4 changes: 2 additions & 2 deletions crx/capo.js

Large diffs are not rendered by default.

47 changes: 29 additions & 18 deletions crx/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ init();

async function getCurrentTab() {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return {tabId: tab.id};
return { tabId: tab.id };
}

async function init() {
await chrome.storage.local.remove('data');
await chrome.storage.local.remove("data");

await chrome.scripting.executeScript({
target: await getCurrentTab(),
files: ['capo.js']
})
files: ["capo.js"],
});

chrome.storage.onChanged.addListener((changes) => {
console.log('Storage changed', changes)
const {data} = changes;
console.log("Storage changed", changes);
const { data } = changes;
if (data?.newValue) {
print(data.newValue);
}
});
}

function print(result) {
console.log('Data', result);
console.log("Data", result);
let frag = document.createDocumentFragment();
for (let r of result.actual) {
frag.appendChild(getCapoHeadElement(r));
Expand All @@ -38,13 +38,20 @@ function print(result) {
frag.appendChild(getCapoHeadElement(r));
}
sorted.appendChild(frag);
document.body.addEventListener('click', handleCapoClick);
document.body.addEventListener("click", handleCapoClick);
}

function getCapoHeadElement({weight, color, selector, innerHTML, isValid, customValidations}) {
const span = document.createElement('span');
span.classList.add('capo-head-element');
span.classList.toggle('invalid', !isValid);
function getCapoHeadElement({
weight,
color,
selector,
innerHTML,
isValid,
customValidations,
}) {
const span = document.createElement("span");
span.classList.add("capo-head-element");
span.classList.toggle("invalid", !isValid);
span.dataset.weight = weight;
span.style.backgroundColor = color;
span.dataset.selector = selector;
Expand All @@ -55,17 +62,21 @@ function getCapoHeadElement({weight, color, selector, innerHTML, isValid, custom
}

async function handleCapoClick(event) {
const {weight, selector, innerHTML} = event.target.dataset;
const { weight, selector, innerHTML } = event.target.dataset;
const customValidations = JSON.parse(event.target.dataset.customValidations);
const isValid = !event.target.classList.contains('invalid');
const isValid = !event.target.classList.contains("invalid");

await chrome.storage.local.set({
click: JSON.stringify({
weight, selector, innerHTML, isValid, customValidations
})
weight,
selector,
innerHTML,
isValid,
customValidations,
}),
});
await chrome.scripting.executeScript({
target: await getCurrentTab(),
files: ['capo.js']
files: ["capo.js"],
});
}
2 changes: 1 addition & 1 deletion crx/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Capo: get your ﹤𝚑𝚎𝚊𝚍﹥ in order",
"description": "Visualize the optimal ordering of ﹤𝚑𝚎𝚊𝚍﹥ elements on any web page",
"version": "1.4.7",
"version": "1.4.8",
"permissions": [
"scripting",
"activeTab",
Expand Down
8 changes: 6 additions & 2 deletions docs/src/lib/capo.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class $33f7359dc421be0c$export$8f8422ac5947a789 {
}
try {
let html = await this.getStaticHTML();
html = html.replace(/(\<\/?)(head)/ig, "$1static-head");
html = html.replace(/(\<\/?)(head)/gi, "$1static-head");
const staticDoc = this.document.implementation.createHTMLDocument("New Document");
staticDoc.documentElement.innerHTML = html;
this.head = staticDoc.querySelector("static-head");
Expand Down Expand Up @@ -155,7 +155,11 @@ class $33f7359dc421be0c$export$8f8422ac5947a789 {
return;
}
const { payload: payload, warnings: warnings } = customValidations;
if (payload) args.push(payload);
if (payload) {
if (typeof payload.expiry == "string") // Deserialize origin trial expiration dates.
payload.expiry = new Date(payload.expiry);
args.push(payload);
}
if (warnings?.length) {
// Element-specific warnings.
loggingLevel = "warn";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rviscomi/capo.js",
"version": "1.4.6",
"version": "1.4.8",
"description": "Get your ﹤𝚑𝚎𝚊𝚍﹥ in order",
"author": "Rick Viscomi",
"license": "Apache-2.0",
Expand Down
8 changes: 6 additions & 2 deletions snippet/capo.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class $d410929ede0a2ee4$export$8f8422ac5947a789 {
}
try {
let html = await this.getStaticHTML();
html = html.replace(/(\<\/?)(head)/ig, "$1static-head");
html = html.replace(/(\<\/?)(head)/gi, "$1static-head");
const staticDoc = this.document.implementation.createHTMLDocument("New Document");
staticDoc.documentElement.innerHTML = html;
this.head = staticDoc.querySelector("static-head");
Expand Down Expand Up @@ -156,7 +156,11 @@ class $d410929ede0a2ee4$export$8f8422ac5947a789 {
return;
}
const { payload: payload, warnings: warnings } = customValidations;
if (payload) args.push(payload);
if (payload) {
if (typeof payload.expiry == "string") // Deserialize origin trial expiration dates.
payload.expiry = new Date(payload.expiry);
args.push(payload);
}
if (warnings?.length) {
// Element-specific warnings.
loggingLevel = "warn";
Expand Down
39 changes: 24 additions & 15 deletions src/crx/capo.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import * as capo from '../main.js';
import * as logging from '../snippet/logging.js';
import * as capo from "../main.js";
import * as logging from "../snippet/logging.js";

async function run(io) {
await io.init();
logging.validateHead(io, capo.validation);
const headWeights = logging.logWeights(io, capo.validation, capo.rules);

return {
actual: headWeights.map(({element, weight, isValid, customValidations}) => ({
weight,
color: io.getColor(weight),
selector: io.stringifyElement(element),
innerHTML: element.innerHTML,
isValid,
customValidations
})),
actual: headWeights.map(
({ element, weight, isValid, customValidations }) => {
if (customValidations?.payload?.expiry) {
// Serialize origin trial expiration dates.
customValidations.payload.expiry =
customValidations.payload.expiry.toString();
}
return {
weight,
color: io.getColor(weight),
selector: io.stringifyElement(element),
innerHTML: element.innerHTML,
isValid,
customValidations,
};
}
),
};
}

async function initOptions() {
const {options} = await chrome.storage.sync.get('options');
const { options } = await chrome.storage.sync.get("options");
return new capo.options.Options(options);
}

Expand All @@ -33,16 +42,16 @@ async function init() {
// 2. User clicks an element in the color bar
//
// The existence of the click object tells us which scenario we're in.
const {click} = await chrome.storage.local.get('click');
const { click } = await chrome.storage.local.get("click");
if (click) {
io.logElementFromSelector(JSON.parse(click));
await chrome.storage.local.remove('click');
await chrome.storage.local.remove("click");
} else {
const data = await run(io);
await chrome.storage.local.set({
data: data
data: data,
});
}
}
}

init();
Loading

0 comments on commit 3968270

Please sign in to comment.