Skip to content

Commit 943d9ca

Browse files
authored
Extract Apple Config processing functions (#20)
* Modify apple inject script for modularity * Modify post install scripts to use correct paths * Refactor build process to rollup scripts. Skip copy sjcl if it doesn't exist
1 parent b4989d5 commit 943d9ca

File tree

7 files changed

+231
-207
lines changed

7 files changed

+231
-207
lines changed

build/apple/contentScope.js

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,56 @@
1-
var contentScopeFeatures = (function (exports) {
1+
(function () {
2+
'use strict';
3+
4+
function getTopLevelURL () {
5+
try {
6+
// FROM: https://stackoverflow.com/a/7739035/73479
7+
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
8+
if (window.location !== window.parent.location) {
9+
return new URL(window.location.href !== 'about:blank' ? document.referrer : window.parent.location.href)
10+
} else {
11+
return new URL(window.location.href)
12+
}
13+
} catch (error) {
14+
return new URL(location.href)
15+
}
16+
}
17+
18+
function isUnprotectedDomain (topLevelUrl, featureList) {
19+
let unprotectedDomain = false;
20+
const domainParts = topLevelUrl && topLevelUrl.host ? topLevelUrl.host.split('.') : [];
21+
22+
// walk up the domain to see if it's unprotected
23+
while (domainParts.length > 1 && !unprotectedDomain) {
24+
const partialDomain = domainParts.join('.');
25+
26+
unprotectedDomain = featureList.filter(domain => domain.domain === partialDomain).length > 0;
27+
28+
domainParts.shift();
29+
}
30+
31+
return unprotectedDomain
32+
}
33+
34+
function processConfig (data, userList, preferences) {
35+
const topLevelUrl = getTopLevelURL();
36+
const allowlisted = userList.filter(domain => domain === topLevelUrl.host).length > 0;
37+
const enabledFeatures = Object.keys(data.features).filter((featureName) => {
38+
const feature = data.features[featureName];
39+
return feature.state === 'enabled' && !isUnprotectedDomain(topLevelUrl, feature.exceptions)
40+
});
41+
const isBroken = isUnprotectedDomain(topLevelUrl, data.unprotectedTemporary);
42+
preferences.site = {
43+
domain: topLevelUrl.hostname,
44+
isBroken,
45+
allowlisted,
46+
enabledFeatures
47+
};
48+
// TODO
49+
preferences.cookie = {};
50+
return preferences
51+
}
52+
53+
var contentScopeFeatures = (function (exports) {
254
'use strict';
355

456
const sjcl = (() => {
@@ -2852,68 +2904,21 @@ var contentScopeFeatures = (function (exports) {
28522904
})({});
28532905

28542906

2855-
function getTopLevelURL () {
2856-
try {
2857-
// FROM: https://stackoverflow.com/a/7739035/73479
2858-
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
2859-
if (window.location !== window.parent.location) {
2860-
return new URL(window.location.href !== 'about:blank' ? document.referrer : window.parent.location.href)
2861-
} else {
2862-
return new URL(window.location.href)
2907+
function init () {
2908+
const processedConfig = processConfig($CONTENT_SCOPE$, $USER_UNPROTECTED_DOMAINS$, $USER_PREFERENCES$);
2909+
if (processedConfig.site.allowlisted) {
2910+
return
28632911
}
2864-
} catch (error) {
2865-
return new URL(location.href)
2866-
}
2867-
}
2868-
2869-
function isUnprotectedDomain (topLevelUrl, featureList) {
2870-
let unprotectedDomain = false
2871-
const domainParts = topLevelUrl && topLevelUrl.host ? topLevelUrl.host.split('.') : []
28722912

2873-
// walk up the domain to see if it's unprotected
2874-
while (domainParts.length > 1 && !unprotectedDomain) {
2875-
const partialDomain = domainParts.join('.')
2913+
contentScopeFeatures.load();
28762914

2877-
unprotectedDomain = featureList.filter(domain => domain.domain === partialDomain).length > 0
2915+
contentScopeFeatures.init(processedConfig);
28782916

2879-
domainParts.shift()
2917+
// Not supported:
2918+
// contentScopeFeatures.update(message)
28802919
}
28812920

2882-
return unprotectedDomain
2883-
}
2884-
2885-
function processConfig (data, userList, preferences) {
2886-
const topLevelUrl = getTopLevelURL()
2887-
const allowlisted = userList.filter(domain => domain === topLevelUrl.host).length > 0
2888-
const enabledFeatures = Object.keys(data.features).filter((featureName) => {
2889-
const feature = data.features[featureName]
2890-
return feature.state === 'enabled' && !isUnprotectedDomain(topLevelUrl, feature.exceptions)
2891-
})
2892-
const isBroken = isUnprotectedDomain(topLevelUrl, data.unprotectedTemporary)
2893-
preferences.site = {
2894-
domain: topLevelUrl.hostname,
2895-
isBroken,
2896-
allowlisted,
2897-
enabledFeatures
2898-
}
2899-
// TODO
2900-
preferences.cookie = {}
2901-
return preferences
2902-
}
2903-
2904-
function init () {
2905-
const processedConfig = processConfig($CONTENT_SCOPE$, $USER_UNPROTECTED_DOMAINS$, $USER_PREFERENCES$)
2906-
if (processedConfig.site.allowlisted) {
2907-
return
2908-
}
2909-
2910-
contentScopeFeatures.load()
2911-
2912-
contentScopeFeatures.init(processedConfig)
2913-
2914-
// Not supported:
2915-
// contentScopeFeatures.update(message)
2916-
}
2921+
init();
29172922

2918-
init()
2923+
})();
29192924

build/firefox/inject.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
var contentScopeFeatures = (function (exports) {
1+
(function () {
2+
'use strict';
3+
4+
var contentScopeFeatures = (function (exports) {
25
'use strict';
36

47
const sjcl = (() => {
@@ -2868,40 +2871,42 @@ var contentScopeFeatures = (function (exports) {
28682871
})({});
28692872

28702873

2871-
function init () {
2872-
contentScopeFeatures.load()
2873-
2874-
chrome.runtime.sendMessage({
2875-
messageType: 'registeredContentScript',
2876-
options: {
2877-
documentUrl: window.location.href
2878-
}
2879-
},
2880-
(message) => {
2881-
// Background has disabled features
2882-
if (!message) {
2883-
return
2884-
}
2885-
if (message.debug) {
2886-
window.addEventListener('message', (m) => {
2887-
if (m.data.action && m.data.message) {
2888-
chrome.runtime.sendMessage({
2889-
debuggerMessage: m.data
2890-
})
2891-
}
2892-
})
2874+
function init () {
2875+
contentScopeFeatures.load();
2876+
2877+
chrome.runtime.sendMessage({
2878+
messageType: 'registeredContentScript',
2879+
options: {
2880+
documentUrl: window.location.href
2881+
}
2882+
},
2883+
(message) => {
2884+
// Background has disabled features
2885+
if (!message) {
2886+
return
2887+
}
2888+
if (message.debug) {
2889+
window.addEventListener('message', (m) => {
2890+
if (m.data.action && m.data.message) {
2891+
chrome.runtime.sendMessage({
2892+
debuggerMessage: m.data
2893+
});
2894+
}
2895+
});
2896+
}
2897+
contentScopeFeatures.init(message);
28932898
}
2894-
contentScopeFeatures.init(message)
2899+
);
2900+
2901+
chrome.runtime.onMessage.addListener((message) => {
2902+
// forward update messages to the embedded script
2903+
if (message && message.type === 'update') {
2904+
contentScopeFeatures.update(message);
2905+
}
2906+
});
28952907
}
2896-
)
28972908

2898-
chrome.runtime.onMessage.addListener((message) => {
2899-
// forward update messages to the embedded script
2900-
if (message && message.type === 'update') {
2901-
contentScopeFeatures.update(message)
2902-
}
2903-
})
2904-
}
2909+
init();
29052910

2906-
init()
2911+
})();
29072912

build/integration/contentScope.js

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
var contentScopeFeatures = (function (exports) {
1+
(function () {
2+
'use strict';
3+
4+
var contentScopeFeatures = (function (exports) {
25
'use strict';
36

47
const sjcl = (() => {
@@ -2852,73 +2855,75 @@ var contentScopeFeatures = (function (exports) {
28522855
})({});
28532856

28542857

2855-
function getTopLevelURL () {
2856-
try {
2857-
// FROM: https://stackoverflow.com/a/7739035/73479
2858-
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
2859-
if (window.location !== window.parent.location) {
2860-
return new URL(window.location.href !== 'about:blank' ? document.referrer : window.parent.location.href)
2861-
} else {
2862-
return new URL(window.location.href)
2858+
function getTopLevelURL () {
2859+
try {
2860+
// FROM: https://stackoverflow.com/a/7739035/73479
2861+
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
2862+
if (window.location !== window.parent.location) {
2863+
return new URL(window.location.href !== 'about:blank' ? document.referrer : window.parent.location.href)
2864+
} else {
2865+
return new URL(window.location.href)
2866+
}
2867+
} catch (error) {
2868+
return new URL(location.href)
28632869
}
2864-
} catch (error) {
2865-
return new URL(location.href)
28662870
}
2867-
}
2868-
2869-
function generateConfig (data, userList) {
2870-
const topLevelUrl = getTopLevelURL()
2871-
return {
2872-
debug: false,
2873-
sessionKey: 'randomVal',
2874-
site: {
2875-
domain: topLevelUrl.hostname,
2876-
isBroken: false,
2877-
allowlisted: false,
2878-
enabledFeatures: [
2879-
'fingerprintingCanvas',
2880-
'fingerprintingScreenSize',
2881-
'navigatorInterface'
2882-
]
2871+
2872+
function generateConfig (data, userList) {
2873+
const topLevelUrl = getTopLevelURL();
2874+
return {
2875+
debug: false,
2876+
sessionKey: 'randomVal',
2877+
site: {
2878+
domain: topLevelUrl.hostname,
2879+
isBroken: false,
2880+
allowlisted: false,
2881+
enabledFeatures: [
2882+
'fingerprintingCanvas',
2883+
'fingerprintingScreenSize',
2884+
'navigatorInterface'
2885+
]
2886+
}
28832887
}
28842888
}
2885-
}
2886-
2887-
async function init () {
2888-
const topLevelUrl = getTopLevelURL()
2889-
const processedConfig = generateConfig()
2890-
await contentScopeFeatures.load()
28912889

2892-
// mark this phase as loaded
2893-
setStatus('loaded')
2890+
async function init () {
2891+
const topLevelUrl = getTopLevelURL();
2892+
const processedConfig = generateConfig();
2893+
await contentScopeFeatures.load();
28942894

2895-
if (!topLevelUrl.searchParams.has('wait-for-init-args')) {
2896-
await contentScopeFeatures.init(processedConfig)
2897-
setStatus('initialized')
2898-
return
2899-
}
2895+
// mark this phase as loaded
2896+
setStatus('loaded');
29002897

2901-
// Wait for a message containing additional config
2902-
document.addEventListener('content-scope-init-args', async (evt) => {
2903-
const merged = {
2904-
...processedConfig,
2905-
...evt.detail
2898+
if (!topLevelUrl.searchParams.has('wait-for-init-args')) {
2899+
await contentScopeFeatures.init(processedConfig);
2900+
setStatus('initialized');
2901+
return
29062902
}
29072903

2908-
// init features
2909-
await contentScopeFeatures.init(merged)
2904+
// Wait for a message containing additional config
2905+
document.addEventListener('content-scope-init-args', async (evt) => {
2906+
const merged = {
2907+
...processedConfig,
2908+
...evt.detail
2909+
};
29102910

2911-
// set status to initialized so that tests can resume
2912-
setStatus('initialized')
2913-
}, { once: true })
2914-
}
2911+
// init features
2912+
await contentScopeFeatures.init(merged);
2913+
2914+
// set status to initialized so that tests can resume
2915+
setStatus('initialized');
2916+
}, { once: true });
2917+
}
2918+
2919+
/**
2920+
* @param {"loaded" | "initialized"} status
2921+
*/
2922+
function setStatus (status) {
2923+
window.__content_scope_status = status;
2924+
}
29152925

2916-
/**
2917-
* @param {"loaded" | "initialized"} status
2918-
*/
2919-
function setStatus (status) {
2920-
window.__content_scope_status = status
2921-
}
2926+
init();
29222927

2923-
init()
2928+
})();
29242929

0 commit comments

Comments
 (0)