diff --git a/source/background.js b/source/background.js index 7f70157..382a976 100644 --- a/source/background.js +++ b/source/background.js @@ -2,7 +2,9 @@ import addDomainPermissionToggle from 'webext-permission-toggle'; import oneEvent from 'one-event'; import optionsStorage from './options-storage.js'; -const browserAction = chrome.action ?? chrome.browserAction; +const browser = globalThis.browser ?? globalThis.chrome; + +const browserAction = browser.action ?? browser.browserAction; // Firefox hates iframes on activeTab // https://bugzilla.mozilla.org/show_bug.cgi?id=1653408 @@ -17,17 +19,22 @@ if (navigator.userAgent.includes('Firefox/')) { } function stopGT(tab) { - chrome.scripting.executeScript({ + browser.scripting.executeScript({ target: {tabId: tab.id}, func: () => stopGT(), }); } async function handleAction({id}) { - const defaults = { + const defaultTarget = { target: {tabId: id, allFrames: true}, }; - const [topFrame] = await chrome.scripting.executeScript({ + const defaults = { + ...defaultTarget, + injectImmediately: true, + }; + + const [topFrame] = await browser.scripting.executeScript({ ...defaults, func: () => typeof window.startGT === 'function', }); @@ -37,15 +44,15 @@ async function handleAction({id}) { if (!alreadyInjected) { try { await Promise.all([ - chrome.scripting.insertCSS({...defaults, files: ['/ghost-text.css']}), - chrome.scripting.executeScript({...defaults, files: ['/ghost-text.js']}), + browser.scripting.insertCSS({...defaultTarget, files: ['/ghost-text.css']}), + browser.scripting.executeScript({...defaults, files: ['/ghost-text.js']}), ]); } catch (error) { console.error(error); } } - await chrome.scripting.executeScript({...defaults, func: () => startGT()}); + await browser.scripting.executeScript({...defaults, func: () => startGT()}); } function handlePortListenerErrors(listener) { @@ -69,7 +76,7 @@ function handlePortListenerErrors(listener) { }; } -chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => { +browser.runtime.onConnect.addListener(handlePortListenerErrors(async port => { console.assert(port.name === 'new-field'); const options = await optionsStorage.getAll(); const response = await fetch(`http://localhost:${options.serverPort}`); @@ -115,50 +122,50 @@ function handleMessages({code, count}, {tab}) { tabId: tab.id, }); } else if (code === 'focus-tab') { - chrome.tabs.update(tab.id, {active: true}); - chrome.windows.update(tab.windowId, {focused: true}); + browser.tabs.update(tab.id, {active: true}); + browser.windows.update(tab.windowId, {focused: true}); } } // Temporary code from https://github.com/fregante/GhostText/pull/267 async function saveShortcut() { - const storage = await chrome.storage.local.get('shortcut'); + const storage = await browser.storage.local.get('shortcut'); if (storage.shortcut) { // Already saved return; } - const shortcuts = await chrome.commands.getAll(); + const shortcuts = await browser.commands.getAll(); for (const item of shortcuts) { if (item.shortcut) { // eslint-disable-next-line no-await-in-loop -- Intentional - await chrome.storage.local.set({shortcut: item.shortcut}); + await browser.storage.local.set({shortcut: item.shortcut}); return; } } } async function getActiveTab() { - const [activeTab] = await chrome.tabs.query({active: true, currentWindow: true}); + const [activeTab] = await browser.tabs.query({active: true, currentWindow: true}); return activeTab; } function init() { browserAction.onClicked.addListener(handleAction); - chrome.runtime.onMessage.addListener(handleMessages); - chrome.contextMenus.create({ + browser.runtime.onMessage.addListener(handleMessages); + browser.contextMenus.create({ id: 'stop-gt', title: 'Disconnect GhostText on this page', contexts: ['action'], }); - chrome.contextMenus.onClicked.addListener(({menuItemId}, tab) => { + browser.contextMenus.onClicked.addListener(({menuItemId}, tab) => { if (menuItemId === 'stop-gt') { stopGT(tab); } else if (menuItemId === 'start-gt-editable') { handleAction(tab); } }); - chrome.commands.onCommand.addListener(async (command, tab = getActiveTab()) => { + browser.commands.onCommand.addListener(async (command, tab = getActiveTab()) => { if (command === 'open') { handleAction(await tab); } else if (command === 'close') { @@ -166,7 +173,7 @@ function init() { } }); - chrome.contextMenus.create({ + browser.contextMenus.create({ id: 'start-gt-editable', title: 'Activate GhostText on field', contexts: ['editable'], @@ -176,15 +183,15 @@ function init() { color: '#008040', }); - chrome.runtime.onInstalled.addListener(async ({reason}) => { + browser.runtime.onInstalled.addListener(async ({reason}) => { // Only notify on install if (reason === 'install') { - const {installType} = await chrome.management.getSelf(); + const {installType} = await browser.management.getSelf(); if (installType === 'development') { return; } - await chrome.tabs.create({ + await browser.tabs.create({ url: 'https://ghosttext.fregante.com/welcome/', active: true, }); diff --git a/source/manifest.json b/source/manifest.json index aca33c7..ac8079e 100644 --- a/source/manifest.json +++ b/source/manifest.json @@ -14,6 +14,9 @@ "128": "icons/icon128.png", "256": "icons/icon256.png" }, + "content_security_policy": { + "extension_pages": "connect-src 'self' http://localhost:* ws:; script-src 'self';" + }, "permissions": [ "activeTab", "contextMenus",