diff --git a/manifest.json b/manifest.json index 8c3f310..faccaa5 100644 --- a/manifest.json +++ b/manifest.json @@ -24,7 +24,8 @@ "permissions": [ "storage", "tabs", - "webNavigation" + "webNavigation", + "scripting" ], "background": { "service_worker": "service_workers/background.js", "type": "module" @@ -33,5 +34,6 @@ "web_accessible_resources": [{ "matches": [""], "resources": ["utils.js"] - }] + }], + "host_permissions": ["https://*.twitter.com/*"] } diff --git a/service_workers/background.js b/service_workers/background.js index 0d3ae03..6623c3a 100644 --- a/service_workers/background.js +++ b/service_workers/background.js @@ -8,18 +8,39 @@ chrome.webNavigation.onCommitted.addListener(async () => { }) chrome.runtime.onInstalled.addListener(async () => { + const openTwitterTabs = await chrome.tabs.query({ url: 'https://*.twitter.com/*' }) + const versionInStorage = await chrome.storage.sync.get('version') - if(Object.keys(versionInStorage).length === 0) { + const foundVersionInStorage = Object.keys(versionInStorage).length === 1 + if(!foundVersionInStorage) { await chrome.storage.sync.clear() setDefaultRules() + + injectContentScriptInOpenTwitterTabs(openTwitterTabs) + return } const rulesInStorage = await chrome.storage.sync.get('CSSRulesArrayOfObjectsWithNames') const foundStoredRules = Object.keys(rulesInStorage).length > 0 if (!foundStoredRules) setDefaultRules() + + injectContentScriptInOpenTwitterTabs(openTwitterTabs) }) +/** + * + * @param {chrome.tabs.Tab[]} tabs + */ +function injectContentScriptInOpenTwitterTabs(tabs) { + for (const tab of tabs) { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + files: ['scripts/content.js'] + }) + } +} + chrome.storage.onChanged.addListener((changes, areaName) => { let [[key, { oldValue, newValue }]] = Object.entries(changes);