-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ba3096
commit 6ae1b13
Showing
6 changed files
with
119 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { setRequestHeaders, unsetRequestHeaders } from './http-requests' | ||
export { setBridgeData, unsetBridgeData } from './content-script-bridge' | ||
export { registerContentScripts } from './scripting' |
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,41 @@ | ||
import RegisteredContentScript = chrome.scripting.RegisteredContentScript | ||
|
||
// the common properties for the content scripts | ||
const common: Omit<RegisteredContentScript, 'id'> = { | ||
matches: ['<all_urls>'], | ||
allFrames: true, | ||
runAt: 'document_start', | ||
} | ||
|
||
// properties for the content script that will be executed in the isolated world (as a content script) | ||
const content: RegisteredContentScript = { ...common, id: 'content', js: ['content.js'] } | ||
|
||
// properties for the content script that will be executed in the main world (as an injected script) | ||
const inject: RegisteredContentScript = { ...common, id: 'inject', js: [__UNIQUE_INJECT_FILENAME__] } | ||
|
||
/** Register the content scripts */ | ||
export async function registerContentScripts() { | ||
// first, unregister (probably) previously registered content scripts | ||
await chrome.scripting.unregisterContentScripts() | ||
|
||
try { | ||
await chrome.scripting.registerContentScripts([ | ||
{ ...content, world: 'ISOLATED' }, | ||
{ ...inject, world: 'MAIN' }, | ||
]) | ||
} catch (err) { | ||
if ( | ||
err instanceof Error && | ||
err.message.toLowerCase().includes('unexpected property') && | ||
err.message.includes('world') | ||
) { | ||
// if so, it means that the "world" property is not supported by the current browser (FireFox at this moment) | ||
// so we need to register the content scripts without the "world" property | ||
// | ||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/RegisteredContentScript#browser_compatibility | ||
return await chrome.scripting.registerContentScripts([content, inject]) | ||
} | ||
|
||
throw err | ||
} | ||
} |
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