Skip to content

Commit

Permalink
Update injection because it's not using webext-content-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jun 30, 2024
1 parent e79738a commit 82e0e74
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,35 @@ if (navigator.userAgent.includes('Firefox/')) {
}

function stopGT(tab) {
chrome.scripting.executeScript(tab.id, {
code: 'stopGT()',
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: () => stopGT(),
});
}

async function handleAction({id}) {
const defaults = {
runAt: 'document_start',
allFrames: true,
target: {tabId: id, allFrames: true},
};
const [alreadyInjected] = await chrome.scripting.executeScript(id, {
const [topFrame] = await chrome.scripting.executeScript({
...defaults,
code: 'typeof window.startGT === "function"',
func: () => typeof window.startGT === 'function',
});
if (alreadyInjected) {
return chrome.scripting.executeScript(id, {...defaults, code: 'startGT()'});
}

try {
await Promise.all([
chrome.scripting.insertCSS(id, {...defaults, file: '/ghost-text.css'}),
chrome.scripting.executeScript(id, {...defaults, file: '/ghost-text.js'}),
]);
} catch (error) {
console.error(error);
const alreadyInjected = topFrame.result;

if (!alreadyInjected) {
try {
await Promise.all([
chrome.scripting.insertCSS({...defaults, files: ['/ghost-text.css']}),
chrome.scripting.executeScript({...defaults, files: ['/ghost-text.js']}),
]);
} catch (error) {
console.error(error);
}
}

await chrome.scripting.executeScript(id, {...defaults, code: 'startGT()'});
await chrome.scripting.executeScript({...defaults, func: () => startGT()});

Check failure on line 48 in source/background.js

View workflow job for this annotation

GitHub Actions / Lint

'startGT' is not defined.

Check failure on line 48 in source/background.js

View workflow job for this annotation

GitHub Actions / Lint

'startGT' is not defined.
}

function handlePortListenerErrors(listener) {
Expand Down

0 comments on commit 82e0e74

Please sign in to comment.