Skip to content

Commit 3968270

Browse files
authored
Serialize origin trial expiration dates (#98)
1 parent abf1f48 commit 3968270

File tree

8 files changed

+171
-93
lines changed

8 files changed

+171
-93
lines changed

crx/capo.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crx/chrome.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ init();
22

33
async function getCurrentTab() {
44
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
5-
return {tabId: tab.id};
5+
return { tabId: tab.id };
66
}
77

88
async function init() {
9-
await chrome.storage.local.remove('data');
9+
await chrome.storage.local.remove("data");
1010

1111
await chrome.scripting.executeScript({
1212
target: await getCurrentTab(),
13-
files: ['capo.js']
14-
})
13+
files: ["capo.js"],
14+
});
1515

1616
chrome.storage.onChanged.addListener((changes) => {
17-
console.log('Storage changed', changes)
18-
const {data} = changes;
17+
console.log("Storage changed", changes);
18+
const { data } = changes;
1919
if (data?.newValue) {
2020
print(data.newValue);
2121
}
2222
});
2323
}
24-
24+
2525
function print(result) {
26-
console.log('Data', result);
26+
console.log("Data", result);
2727
let frag = document.createDocumentFragment();
2828
for (let r of result.actual) {
2929
frag.appendChild(getCapoHeadElement(r));
@@ -38,13 +38,20 @@ function print(result) {
3838
frag.appendChild(getCapoHeadElement(r));
3939
}
4040
sorted.appendChild(frag);
41-
document.body.addEventListener('click', handleCapoClick);
41+
document.body.addEventListener("click", handleCapoClick);
4242
}
4343

44-
function getCapoHeadElement({weight, color, selector, innerHTML, isValid, customValidations}) {
45-
const span = document.createElement('span');
46-
span.classList.add('capo-head-element');
47-
span.classList.toggle('invalid', !isValid);
44+
function getCapoHeadElement({
45+
weight,
46+
color,
47+
selector,
48+
innerHTML,
49+
isValid,
50+
customValidations,
51+
}) {
52+
const span = document.createElement("span");
53+
span.classList.add("capo-head-element");
54+
span.classList.toggle("invalid", !isValid);
4855
span.dataset.weight = weight;
4956
span.style.backgroundColor = color;
5057
span.dataset.selector = selector;
@@ -55,17 +62,21 @@ function getCapoHeadElement({weight, color, selector, innerHTML, isValid, custom
5562
}
5663

5764
async function handleCapoClick(event) {
58-
const {weight, selector, innerHTML} = event.target.dataset;
65+
const { weight, selector, innerHTML } = event.target.dataset;
5966
const customValidations = JSON.parse(event.target.dataset.customValidations);
60-
const isValid = !event.target.classList.contains('invalid');
67+
const isValid = !event.target.classList.contains("invalid");
6168

6269
await chrome.storage.local.set({
6370
click: JSON.stringify({
64-
weight, selector, innerHTML, isValid, customValidations
65-
})
71+
weight,
72+
selector,
73+
innerHTML,
74+
isValid,
75+
customValidations,
76+
}),
6677
});
6778
await chrome.scripting.executeScript({
6879
target: await getCurrentTab(),
69-
files: ['capo.js']
80+
files: ["capo.js"],
7081
});
7182
}

crx/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "Capo: get your ﹤𝚑𝚎𝚊𝚍﹥ in order",
44
"description": "Visualize the optimal ordering of ﹤𝚑𝚎𝚊𝚍﹥ elements on any web page",
5-
"version": "1.4.7",
5+
"version": "1.4.8",
66
"permissions": [
77
"scripting",
88
"activeTab",

docs/src/lib/capo.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class $33f7359dc421be0c$export$8f8422ac5947a789 {
6767
}
6868
try {
6969
let html = await this.getStaticHTML();
70-
html = html.replace(/(\<\/?)(head)/ig, "$1static-head");
70+
html = html.replace(/(\<\/?)(head)/gi, "$1static-head");
7171
const staticDoc = this.document.implementation.createHTMLDocument("New Document");
7272
staticDoc.documentElement.innerHTML = html;
7373
this.head = staticDoc.querySelector("static-head");
@@ -155,7 +155,11 @@ class $33f7359dc421be0c$export$8f8422ac5947a789 {
155155
return;
156156
}
157157
const { payload: payload, warnings: warnings } = customValidations;
158-
if (payload) args.push(payload);
158+
if (payload) {
159+
if (typeof payload.expiry == "string") // Deserialize origin trial expiration dates.
160+
payload.expiry = new Date(payload.expiry);
161+
args.push(payload);
162+
}
159163
if (warnings?.length) {
160164
// Element-specific warnings.
161165
loggingLevel = "warn";

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rviscomi/capo.js",
3-
"version": "1.4.6",
3+
"version": "1.4.8",
44
"description": "Get your ﹤𝚑𝚎𝚊𝚍﹥ in order",
55
"author": "Rick Viscomi",
66
"license": "Apache-2.0",

snippet/capo.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class $d410929ede0a2ee4$export$8f8422ac5947a789 {
6868
}
6969
try {
7070
let html = await this.getStaticHTML();
71-
html = html.replace(/(\<\/?)(head)/ig, "$1static-head");
71+
html = html.replace(/(\<\/?)(head)/gi, "$1static-head");
7272
const staticDoc = this.document.implementation.createHTMLDocument("New Document");
7373
staticDoc.documentElement.innerHTML = html;
7474
this.head = staticDoc.querySelector("static-head");
@@ -156,7 +156,11 @@ class $d410929ede0a2ee4$export$8f8422ac5947a789 {
156156
return;
157157
}
158158
const { payload: payload, warnings: warnings } = customValidations;
159-
if (payload) args.push(payload);
159+
if (payload) {
160+
if (typeof payload.expiry == "string") // Deserialize origin trial expiration dates.
161+
payload.expiry = new Date(payload.expiry);
162+
args.push(payload);
163+
}
160164
if (warnings?.length) {
161165
// Element-specific warnings.
162166
loggingLevel = "warn";

src/crx/capo.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
import * as capo from '../main.js';
2-
import * as logging from '../snippet/logging.js';
1+
import * as capo from "../main.js";
2+
import * as logging from "../snippet/logging.js";
33

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

99
return {
10-
actual: headWeights.map(({element, weight, isValid, customValidations}) => ({
11-
weight,
12-
color: io.getColor(weight),
13-
selector: io.stringifyElement(element),
14-
innerHTML: element.innerHTML,
15-
isValid,
16-
customValidations
17-
})),
10+
actual: headWeights.map(
11+
({ element, weight, isValid, customValidations }) => {
12+
if (customValidations?.payload?.expiry) {
13+
// Serialize origin trial expiration dates.
14+
customValidations.payload.expiry =
15+
customValidations.payload.expiry.toString();
16+
}
17+
return {
18+
weight,
19+
color: io.getColor(weight),
20+
selector: io.stringifyElement(element),
21+
innerHTML: element.innerHTML,
22+
isValid,
23+
customValidations,
24+
};
25+
}
26+
),
1827
};
1928
}
2029

2130
async function initOptions() {
22-
const {options} = await chrome.storage.sync.get('options');
31+
const { options } = await chrome.storage.sync.get("options");
2332
return new capo.options.Options(options);
2433
}
2534

@@ -33,16 +42,16 @@ async function init() {
3342
// 2. User clicks an element in the color bar
3443
//
3544
// The existence of the click object tells us which scenario we're in.
36-
const {click} = await chrome.storage.local.get('click');
45+
const { click } = await chrome.storage.local.get("click");
3746
if (click) {
3847
io.logElementFromSelector(JSON.parse(click));
39-
await chrome.storage.local.remove('click');
48+
await chrome.storage.local.remove("click");
4049
} else {
4150
const data = await run(io);
4251
await chrome.storage.local.set({
43-
data: data
52+
data: data,
4453
});
45-
}
54+
}
4655
}
4756

4857
init();

0 commit comments

Comments
 (0)