Skip to content

Commit

Permalink
Use window.eval for executeInPage on Firefox when possible
Browse files Browse the repository at this point in the history
Closes #1287
  • Loading branch information
glacambre committed Jan 9, 2022
1 parent 799188b commit 1a56616
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ export class PageEventEmitter extends EventEmitter<PageEvents, PageHandlers> {
break;
case "get_buf_content":
return new Promise(resolve => this.emit(request.funcName[0], resolve));
case "evalInPage":
case "resizeEditor":
case "getElementContent":
case "getEditorInfo":
// handled by frame function handler
break;
default:
console.error("Unhandled page request:", request);
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ export function isThunderbird() {
// embedding a script element that runs the piece of code and emits its result
// as an event.
export function executeInPage(code: string): Promise<any> {
// On firefox, use an API that allows circumventing some CSP restrictions
// Use wrappedJSObject to detect availability of said API
// DON'T use window.eval on other plateforms - it doesn't have the
// semantics we need!
if ((window as any).wrappedJSObject) {
return new Promise((resolve, reject) => {
try {
resolve(window.eval(code));
} catch (e) {
reject(e);
}
});
}
return new Promise((resolve, reject) => {
const script = document.createElement("script");
const eventId = (new URL(browser.runtime.getURL(""))).hostname + Math.random();
Expand Down

0 comments on commit 1a56616

Please sign in to comment.