-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: browser.devtools.inspectedWindow.eval promise should always reso…
…lve to an array (#175) * fix: browser.devtools.inspectedWindow.eval promise should always resolve to an array * test(integration): Add test for browser.devtools.inspectedWindow.eval API
- Loading branch information
Showing
9 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
let onDevToolsPageLoaded = new Promise(resolve => { | ||
const listener = () => { | ||
browser.runtime.onConnect.removeListener(listener); | ||
resolve(); | ||
}; | ||
browser.runtime.onConnect.addListener(listener); | ||
}); | ||
|
||
browser.runtime.onMessage.addListener(async msg => { | ||
await onDevToolsPageLoaded; | ||
return browser.runtime.sendMessage(msg); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
test("devtools.inspectedWindow.eval resolved with an error result", async (t) => { | ||
const {evalResult} = await browser.runtime.sendMessage({ | ||
apiMethod: "devtools.inspectedWindow.eval", | ||
params: ["throw new Error('fake error');"], | ||
}); | ||
|
||
t.ok(Array.isArray(evalResult), "devtools.inspectedWindow.eval should resolve to an array"); | ||
|
||
t.equal(evalResult[0], navigator.userAgent.includes("Firefox/") ? undefined : null, | ||
"the first element should be null (on chrome) or undefined (on firefox)"); | ||
|
||
t.ok(evalResult[1].isException, "the second element should represent an exception"); | ||
t.ok(evalResult[1].value && evalResult[1].value.includes("fake error"), | ||
"the second element value property should include the expected error message"); | ||
}); | ||
|
||
test("devtools.inspectedWindow.eval resolved without an error result", async (t) => { | ||
const {evalResult} = await browser.runtime.sendMessage({ | ||
apiMethod: "devtools.inspectedWindow.eval", | ||
params: ["[document.documentElement.localName]"], | ||
}); | ||
|
||
t.ok(Array.isArray(evalResult), "devtools.inspectedWindow.eval should resolve to an array"); | ||
|
||
if (navigator.userAgent.includes("Firefox/")) { | ||
t.deepEqual(evalResult, [["html"], undefined], "got the expected values in the array"); | ||
} else { | ||
t.deepEqual(evalResult, [["html"]], "got the expected values in the array"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="browser-polyfill.js"></script> | ||
<script src="devtools_page.js"></script> | ||
</head> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
console.log("devtools page loaded"); | ||
|
||
browser.runtime.onMessage.addListener(async msg => { | ||
switch (msg.apiMethod) { | ||
case "devtools.inspectedWindow.eval": { | ||
const evalResult = await browser.devtools.inspectedWindow.eval(...msg.params); | ||
return {evalResult}; | ||
} | ||
} | ||
|
||
throw new Error(`devtools_page received an unxpected message: ${msg}`); | ||
}); | ||
|
||
browser.runtime.connect({name: "devtools_page"}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "test-extension-devtools-api", | ||
"version": "0.1", | ||
"description": "test-extension-devtools-api", | ||
"content_scripts": [ | ||
{ | ||
"matches": [ | ||
"http://localhost/*" | ||
], | ||
"js": [ | ||
"browser-polyfill.js", | ||
"tape.js", | ||
"content.js" | ||
], | ||
"run_at": "document_end" | ||
} | ||
], | ||
"permissions": [], | ||
"background": { | ||
"scripts": [ | ||
"browser-polyfill.js", | ||
"background.js" | ||
] | ||
}, | ||
"devtools_page": "devtools_page.html" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters