Skip to content
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

fix: statically inject courier content script #238

Merged
merged 1 commit into from
Nov 1, 2024
Merged
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
2 changes: 1 addition & 1 deletion workspace/extension/src/client/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { send } from './runtime.js';
import { index as v4 } from './svelte-4.js';
import { serialize } from './utils.js';

// @ts-ignore - for the app to call with `eval`
// @ts-ignore - https://developer.chrome.com/docs/extensions/how-to/devtools/extend-devtools#selected-element
window['#SvelteDevTools'] = {
/**
* @param {string} id
Expand Down
11 changes: 2 additions & 9 deletions workspace/extension/static/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function courier(tabId, changed) {

chrome.scripting.executeScript({
target: { tabId },

// ensures we're listening to the events before they're dispatched
injectImmediately: true,

// no lexical context, `func` is serialized and deserialized.
Expand All @@ -69,15 +71,6 @@ function courier(tabId, changed) {
// - chrome.i18n
// - chrome.runtime
func: () => {
const source = chrome.runtime.getURL('/courier.js');
if (document.querySelector(`script[src="${source}"]`)) return;

// attach script manually instead of declaring through `files`
// because `detail` in the dispatched custom events is `null`
const script = document.createElement('script');
script.setAttribute('src', source);
document.head.appendChild(script);

chrome.runtime.onMessage.addListener((message, sender) => {
if (sender.id !== chrome.runtime.id) return; // unexpected sender
window.postMessage(message); // relay to content script (courier.js)
Expand Down
8 changes: 8 additions & 0 deletions workspace/extension/static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
"scripts": ["background.js"],
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["courier.js"],
"run_at": "document_start",
"world": "MAIN"
}
],
"devtools_page": "register.html",
"host_permissions": ["*://*/*"],
"permissions": ["activeTab", "scripting"],
Expand Down