Skip to content

Commit 4426f9d

Browse files
Export globals correctly for Firefox (#9)
1 parent 62b1afa commit 4426f9d

File tree

7 files changed

+72
-40
lines changed

7 files changed

+72
-40
lines changed

build/apple/contentScope.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,11 @@ var contentScopeFeatures = (function (exports) {
666666
return sjcl;
667667
})();
668668

669-
/* global exportFunction, false */
669+
/* global cloneInto, exportFunction, false */
670+
671+
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
672+
// eslint-disable-next-line no-global-assign
673+
const globalObj = typeof window === 'undefined' ? globalThis : window;
670674

671675
function getDataKeySync (sessionKey, domainKey, inputData) {
672676
// eslint-disable-next-line new-cap
@@ -822,7 +826,7 @@ var contentScopeFeatures = (function (exports) {
822826
this._native = objectScope[property];
823827
const handler = {};
824828
handler.apply = outputHandler;
825-
this.internal = new globalThis.Proxy(objectScope[property], handler);
829+
this.internal = new globalObj.Proxy(objectScope[property], handler);
826830
}
827831
}
828832

@@ -835,16 +839,19 @@ var contentScopeFeatures = (function (exports) {
835839
}
836840

837841
function postDebugMessage (feature, message) {
838-
globalThis.postMessage({
842+
globalObj.postMessage({
839843
action: feature,
840844
message
841845
});
842846
}
843847

844848
let DDGReflect;
849+
let DDGPromise;
845850

851+
// Exports for usage where we have to cross the xray boundary: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
846852
{
847-
DDGReflect = globalThis.Reflect;
853+
DDGPromise = globalObj.Promise;
854+
DDGReflect = globalObj.Reflect;
848855
}
849856

850857
function __variableDynamicImportRuntime0__(path) {
@@ -2537,8 +2544,8 @@ var contentScopeFeatures = (function (exports) {
25372544
defineProperty(Navigator.prototype, 'duckduckgo', {
25382545
value: {
25392546
platform: args.platform.name,
2540-
async isDuckDuckGo () {
2541-
return true
2547+
isDuckDuckGo () {
2548+
return DDGPromise.resolve(true)
25422549
}
25432550
},
25442551
enumerable: true,

build/chrome/inject.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/firefox/inject.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,11 @@ var contentScopeFeatures = (function (exports) {
666666
return sjcl;
667667
})();
668668

669-
/* global exportFunction, true */
669+
/* global cloneInto, exportFunction, true */
670+
671+
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
672+
// eslint-disable-next-line no-global-assign
673+
const globalObj = typeof window === 'undefined' ? globalThis : window;
670674

671675
function getDataKeySync (sessionKey, domainKey, inputData) {
672676
// eslint-disable-next-line new-cap
@@ -786,12 +790,14 @@ var contentScopeFeatures = (function (exports) {
786790
function defineProperty (object, propertyName, descriptor) {
787791
{
788792
const usedObj = object.wrappedJSObject;
789-
const UsedObjectInterface = globalThis.wrappedJSObject.Object;
793+
const UsedObjectInterface = globalObj.wrappedJSObject.Object;
790794
const definedDescriptor = new UsedObjectInterface();
791795
['configurable', 'enumerable', 'value', 'writable'].forEach((propertyName) => {
792-
// TODO check if value is complex and export it if so.
793796
if (propertyName in descriptor) {
794-
definedDescriptor[propertyName] = descriptor[propertyName];
797+
definedDescriptor[propertyName] = cloneInto(
798+
descriptor[propertyName],
799+
definedDescriptor,
800+
{ cloneFunctions: true });
795801
}
796802
});
797803
['get', 'set'].forEach((methodName) => {
@@ -834,9 +840,9 @@ var contentScopeFeatures = (function (exports) {
834840
};
835841
{
836842
this._native = objectScope[property];
837-
const handler = new globalThis.wrappedJSObject.Object();
838-
handler.apply = exportFunction(outputHandler, globalThis);
839-
this.internal = new globalThis.wrappedJSObject.Proxy(objectScope.wrappedJSObject[property], handler);
843+
const handler = new globalObj.wrappedJSObject.Object();
844+
handler.apply = exportFunction(outputHandler, globalObj);
845+
this.internal = new globalObj.wrappedJSObject.Proxy(objectScope.wrappedJSObject[property], handler);
840846
}
841847
}
842848

@@ -849,16 +855,19 @@ var contentScopeFeatures = (function (exports) {
849855
}
850856

851857
function postDebugMessage (feature, message) {
852-
globalThis.postMessage({
858+
globalObj.postMessage({
853859
action: feature,
854860
message
855861
});
856862
}
857863

858864
let DDGReflect;
865+
let DDGPromise;
859866

867+
// Exports for usage where we have to cross the xray boundary: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
860868
{
861-
DDGReflect = globalThis.wrappedJSObject.Reflect;
869+
DDGPromise = globalObj.wrappedJSObject.Promise;
870+
DDGReflect = globalObj.wrappedJSObject.Reflect;
862871
}
863872

864873
function __variableDynamicImportRuntime0__(path) {
@@ -2551,8 +2560,8 @@ var contentScopeFeatures = (function (exports) {
25512560
defineProperty(Navigator.prototype, 'duckduckgo', {
25522561
value: {
25532562
platform: args.platform.name,
2554-
async isDuckDuckGo () {
2555-
return true
2563+
isDuckDuckGo () {
2564+
return DDGPromise.resolve(true)
25562565
}
25572566
},
25582567
enumerable: true,

build/integration/contentScope.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,11 @@ var contentScopeFeatures = (function (exports) {
666666
return sjcl;
667667
})();
668668

669-
/* global exportFunction, false */
669+
/* global cloneInto, exportFunction, false */
670+
671+
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
672+
// eslint-disable-next-line no-global-assign
673+
const globalObj = typeof window === 'undefined' ? globalThis : window;
670674

671675
function getDataKeySync (sessionKey, domainKey, inputData) {
672676
// eslint-disable-next-line new-cap
@@ -822,7 +826,7 @@ var contentScopeFeatures = (function (exports) {
822826
this._native = objectScope[property];
823827
const handler = {};
824828
handler.apply = outputHandler;
825-
this.internal = new globalThis.Proxy(objectScope[property], handler);
829+
this.internal = new globalObj.Proxy(objectScope[property], handler);
826830
}
827831
}
828832

@@ -835,16 +839,19 @@ var contentScopeFeatures = (function (exports) {
835839
}
836840

837841
function postDebugMessage (feature, message) {
838-
globalThis.postMessage({
842+
globalObj.postMessage({
839843
action: feature,
840844
message
841845
});
842846
}
843847

844848
let DDGReflect;
849+
let DDGPromise;
845850

851+
// Exports for usage where we have to cross the xray boundary: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
846852
{
847-
DDGReflect = globalThis.Reflect;
853+
DDGPromise = globalObj.Promise;
854+
DDGReflect = globalObj.Reflect;
848855
}
849856

850857
function __variableDynamicImportRuntime0__(path) {
@@ -2537,8 +2544,8 @@ var contentScopeFeatures = (function (exports) {
25372544
defineProperty(Navigator.prototype, 'duckduckgo', {
25382545
value: {
25392546
platform: args.platform.name,
2540-
async isDuckDuckGo () {
2541-
return true
2547+
isDuckDuckGo () {
2548+
return DDGPromise.resolve(true)
25422549
}
25432550
},
25442551
enumerable: true,

integration-test/test-navigator-interface.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ describe('Ensure navigator interface is injected', () => {
2525
const isDuckDuckGoResult = await page.evaluate(
2626
() => {
2727
const fn = navigator.duckduckgo?.isDuckDuckGo
28-
const isPromise = fn.constructor.name === 'Promise' || fn.constructor.name === 'AsyncFunction'
29-
return isPromise && fn()
28+
return fn()
3029
}
3130
)
3231
expect(isDuckDuckGoResult).toEqual(true)

src/features/navigator-interface.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineProperty } from '../utils'
1+
import { defineProperty, DDGPromise } from '../utils'
22

33
export function init (args) {
44
try {
@@ -11,8 +11,8 @@ export function init (args) {
1111
defineProperty(Navigator.prototype, 'duckduckgo', {
1212
value: {
1313
platform: args.platform.name,
14-
async isDuckDuckGo () {
15-
return true
14+
isDuckDuckGo () {
15+
return DDGPromise.resolve(true)
1616
}
1717
},
1818
enumerable: true,

src/utils.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
/* global exportFunction, mozProxies */
1+
/* global cloneInto, exportFunction, mozProxies */
22
import { sjcl } from '../lib/sjcl.js'
33

4+
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
5+
// eslint-disable-next-line no-global-assign
6+
const globalObj = typeof window === 'undefined' ? globalThis : window
7+
48
// Tests don't define this variable so fallback to behave like chrome
59
const hasMozProxies = typeof mozProxies !== 'undefined' ? mozProxies : false
610

@@ -122,12 +126,14 @@ export function overrideProperty (name, prop) {
122126
export function defineProperty (object, propertyName, descriptor) {
123127
if (hasMozProxies) {
124128
const usedObj = object.wrappedJSObject
125-
const UsedObjectInterface = globalThis.wrappedJSObject.Object
129+
const UsedObjectInterface = globalObj.wrappedJSObject.Object
126130
const definedDescriptor = new UsedObjectInterface();
127131
['configurable', 'enumerable', 'value', 'writable'].forEach((propertyName) => {
128-
// TODO check if value is complex and export it if so.
129132
if (propertyName in descriptor) {
130-
definedDescriptor[propertyName] = descriptor[propertyName]
133+
definedDescriptor[propertyName] = cloneInto(
134+
descriptor[propertyName],
135+
definedDescriptor,
136+
{ cloneFunctions: true })
131137
}
132138
});
133139
['get', 'set'].forEach((methodName) => {
@@ -172,14 +178,14 @@ export class DDGProxy {
172178
}
173179
if (hasMozProxies) {
174180
this._native = objectScope[property]
175-
const handler = new globalThis.wrappedJSObject.Object()
176-
handler.apply = exportFunction(outputHandler, globalThis)
177-
this.internal = new globalThis.wrappedJSObject.Proxy(objectScope.wrappedJSObject[property], handler)
181+
const handler = new globalObj.wrappedJSObject.Object()
182+
handler.apply = exportFunction(outputHandler, globalObj)
183+
this.internal = new globalObj.wrappedJSObject.Proxy(objectScope.wrappedJSObject[property], handler)
178184
} else {
179185
this._native = objectScope[property]
180186
const handler = {}
181187
handler.apply = outputHandler
182-
this.internal = new globalThis.Proxy(objectScope[property], handler)
188+
this.internal = new globalObj.Proxy(objectScope[property], handler)
183189
}
184190
}
185191

@@ -194,16 +200,20 @@ export class DDGProxy {
194200
}
195201

196202
export function postDebugMessage (feature, message) {
197-
globalThis.postMessage({
203+
globalObj.postMessage({
198204
action: feature,
199205
message
200206
})
201207
}
202208

203209
export let DDGReflect
210+
export let DDGPromise
204211

212+
// Exports for usage where we have to cross the xray boundary: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
205213
if (hasMozProxies) {
206-
DDGReflect = globalThis.wrappedJSObject.Reflect
214+
DDGPromise = globalObj.wrappedJSObject.Promise
215+
DDGReflect = globalObj.wrappedJSObject.Reflect
207216
} else {
208-
DDGReflect = globalThis.Reflect
217+
DDGPromise = globalObj.Promise
218+
DDGReflect = globalObj.Reflect
209219
}

0 commit comments

Comments
 (0)