diff --git a/src/background.js b/src/background.js index a21d2de..822b940 100644 --- a/src/background.js +++ b/src/background.js @@ -14,6 +14,12 @@ async function getLocalStorageValue (key) { }) } +const runtimeInjection = async (settings) => { + window.vitalsLevel = settings.level + window.cruxKey = settings.cruxKey + window.vitalsDevice = settings.vitalsDevice +} + chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { if (changeInfo.status === 'complete' && tab.status === 'complete' && tab.url !== undefined) { const cruxKey = await getLocalStorageValue('apiKey') @@ -23,10 +29,21 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { if (Object.keys(device).length === 0) { device = { deviceSettings: 'PHONE' } } if (Object.keys(level).length === 0) { level = { levelSettings: 'URL' } } - chrome.tabs.executeScript(tab.id, { - code: 'window.vitalsLevel = "' + level.levelSettings + '"; window.cruxKey = "' + cruxKey.apiKey + '"; window.vitalsDevice = "' + device.deviceSettings + '"' - }, function () { - chrome.tabs.executeScript(tab.id, { file: 'bundle.js' }) + const settings = { + cruxKey: cruxKey.apiKey, + level: level.levelSettings, + vitalsDevice: device.deviceSettings + } + + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: runtimeInjection, + args: [settings] + }, () => { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + files: ['bundle.js'] + }) }) } }) diff --git a/src/manifest.json b/src/manifest.json index df1cf5c..d472042 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,17 +1,19 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "Core SERP Vitals", "icons": { "128": "icon128.png"}, "version": "1.2.3", "description": "Show the Core Web Vitals data from the Chrome User Experience Report in the Google Search Results.", "background": { - "scripts" : ["background.js"], - "persistent": true + "service_worker" : "background.js" }, "options_page": "options.html", "permissions": [ "storage", "activeTab", + "scripting" + ], + "host_permissions": [ "https://www.google.com/search?*", "https://www.google.co.uk/search?*", "https://www.google.de/search?*",