Skip to content

Commit

Permalink
Merge pull request #39 from Kenny1291/2-make-it-apply-changes-after-f…
Browse files Browse the repository at this point in the history
…irst-install-without-reloading-twitter-if-user-is-already-on-page

feat: user can enable rules after install without reloading open tabs
  • Loading branch information
Raiquen Guidotti authored Feb 21, 2024
2 parents 61a06b9 + b0d9f9b commit 534d21a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 4 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"permissions": [
"storage",
"tabs",
"webNavigation"
"webNavigation",
"scripting"
],
"background": {
"service_worker": "service_workers/background.js", "type": "module"
Expand All @@ -33,5 +34,6 @@
"web_accessible_resources": [{
"matches": ["<all_urls>"],
"resources": ["utils.js"]
}]
}],
"host_permissions": ["https://*.twitter.com/*"]
}
23 changes: 22 additions & 1 deletion service_workers/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 534d21a

Please sign in to comment.