Skip to content

Jkt/enum block frame conditional #1725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"type": "number",
"value": 222
}
},
"Navigator.prototype.deviceMemory": {
"type": "descriptor",
"getterValue": {
"type": "number",
"value": 1111
}
}
},
"conditionalChanges": [
Expand All @@ -24,6 +31,34 @@
"value": 333
}
]
},
{
"condition": {
"context": {
"top": true
}
},
"patchSettings": [
{
"op": "replace",
"path": "/apiChanges/Navigator.prototype.deviceMemory/getterValue/value",
"value": 43339
}
]
},
{
"condition": {
"context": {
"frame": true
}
},
"patchSettings": [
{
"op": "replace",
"path": "/apiChanges/Navigator.prototype.deviceMemory/getterValue/value",
"value": 43338
}
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@
return results;
});

test('Conditional frame matching', async () => {
const results = [];
const frame = document.createElement('iframe');
const scriptTag = 'script';
frame.srcdoc = `
<!DOCTYPE html>
<html>
<body>
<${scriptTag}>
window.addEventListener('message', (event) => {
if (event.data === 'getDeviceMemory') {
event.source.postMessage(navigator.deviceMemory, event.origin);
}
});
</${scriptTag}>
</body>
</html>
`;
document.body.appendChild(frame);
await new Promise(resolve => frame.onload = resolve);
const deviceMemoryPromise = new Promise(resolve => {
window.addEventListener('message', (event) => {
resolve(event.data);
}, { once: true });
frame.contentWindow.postMessage('getDeviceMemory', '*');
});
const deviceMemory = await deviceMemoryPromise;
results.push({
name: "Ensure iframe changes work",
result: deviceMemory,
expected: 43338
});
results.push({
name: "Expect frame top modification works",
result: navigator.deviceMemory,
expected: 43339
});
return results;
});


// eslint-disable-next-line no-undef
renderResults();
Expand Down
21 changes: 21 additions & 0 deletions injected/src/config-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export default class ConfigFeature {
* @property {object} [experiment]
* @property {string} [experiment.experimentName]
* @property {string} [experiment.cohort]
* @property {object} [context]
* @property {boolean} [context.frame] - true if the condition applies to frames
* @property {boolean} [context.top] - true if the condition applies to the top frame
*/

/**
Expand All @@ -134,6 +137,7 @@ export default class ConfigFeature {
/** @type {Record<string, (conditionBlock: ConditionBlock) => boolean>} */
const conditionChecks = {
domain: this._matchDomainConditional,
context: this._matchContextConditional,
urlPattern: this._matchUrlPatternConditional,
experiment: this._matchExperimentConditional,
};
Expand Down Expand Up @@ -197,6 +201,23 @@ export default class ConfigFeature {
});
}

/**
* Takes a condition block and returns true if the current context matches the context.
* @param {ConditionBlock} conditionBlock
* @returns {boolean}
*/
_matchContextConditional(conditionBlock) {
if (!conditionBlock.context) return false;
const isFrame = window.self !== window.top;
if (conditionBlock.context.frame && isFrame) {
return true;
}
if (conditionBlock.context.top && !isFrame) {
return true;
}
return false;
}

/**
* Takes a condtion block and returns true if the current url matches the urlPattern.
* @param {ConditionBlock} conditionBlock
Expand Down
2 changes: 1 addition & 1 deletion injected/src/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ export const platformSupport = {
],
firefox: ['cookie', ...baseFeatures, 'clickToLoad'],
chrome: ['cookie', ...baseFeatures, 'clickToLoad'],
'chrome-mv3': ['cookie', ...baseFeatures, 'clickToLoad'],
'chrome-mv3': ['cookie', ...baseFeatures, 'clickToLoad', 'webCompat'],
integration: [...baseFeatures, ...otherFeatures],
};
23 changes: 7 additions & 16 deletions injected/src/features/web-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class WebCompat extends ContentFeature {
if (this.getFeatureSettingEnabled('modifyCookies')) {
this.modifyCookies();
}
if (this.getFeatureSettingEnabled('disableDeviceEnumeration') || this.getFeatureSettingEnabled('disableDeviceEnumerationFrames')) {
if (this.getFeatureSettingEnabled('disableDeviceEnumeration')) {
this.preventDeviceEnumeration();
}
}
Expand Down Expand Up @@ -761,21 +761,12 @@ export class WebCompat extends ContentFeature {
if (!window.MediaDevices) {
return;
}
let disableDeviceEnumeration = false;
const isFrame = window.self !== window.top;
if (isFrame) {
disableDeviceEnumeration = this.getFeatureSettingEnabled('disableDeviceEnumerationFrames');
} else {
disableDeviceEnumeration = this.getFeatureSettingEnabled('disableDeviceEnumeration');
}
if (disableDeviceEnumeration) {
const enumerateDevicesProxy = new DDGProxy(this, MediaDevices.prototype, 'enumerateDevices', {
apply() {
return Promise.resolve([]);
},
});
enumerateDevicesProxy.overload();
}
const enumerateDevicesProxy = new DDGProxy(this, MediaDevices.prototype, 'enumerateDevices', {
apply() {
return Promise.resolve([]);
},
});
enumerateDevicesProxy.overload();
}
}

Expand Down
Loading