Skip to content

Commit b6859ac

Browse files
committed
Switch to pageAction (attempt N2)
1 parent a91d401 commit b6859ac

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.DS_Store
2+
archive.zip

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
"name": "Moy.Design",
1212
"description": "Change the looks of your favourite websites. You can either make your own looks or use ones other people made.",
13-
"version": "2.0.1",
13+
"version": "2.0.2",
1414

1515
"icons": {
1616
"128": "res/icon.png"
1717
},
1818

1919
"page_action": {
2020
"default_icon": "res/icon.png",
21-
"show_matches": ["*://*/*"]
21+
"default_title": "Moy.Design"
2222
},
2323

2424
"background": {

src/bg.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const TEMPLATES_DIR = 'MoyTemplates'
3838
const MEDIA_DOMAINS = ['youtube.com', 'youtu.be', 'ytimg.com', 'googlevideo.com', 'vimeo.com', 'vimeocdn.com',
3939
'lj-toys.com', '9cache.com']
4040
const ORIGINAL_LOOK_NAME = 'Original look'
41+
const PAGE_ACTION_URL_PROTOCOLS = ['http:', 'https:']
42+
const PAGE_ACTION_BANNED_URLS = ['https://addons.mozilla.org', 'https://chrome.google.com/webstore']
4143

4244
const AUX_CONTENT_SCRIPTS = ['/lib/handlebars.min.js', '/lib/jquery.slim.min.js', '/src/moyparser.js']
4345
const MAIN_CONTENT_SCRIPT = '/src/cs.js'
@@ -243,6 +245,25 @@ function onDOMContentLoaded(details) {
243245
}
244246
}
245247

248+
function switchPageAction(tabId, url) {
249+
if (0 > tabId && !url) {
250+
return
251+
}
252+
const isProtocolOk = PAGE_ACTION_URL_PROTOCOLS.includes(new URL(url).protocol)
253+
const isUrlBanned = PAGE_ACTION_BANNED_URLS.find(u => url.startsWith(u))
254+
if (isProtocolOk && !isUrlBanned) {
255+
browser.pageAction.show(tabId)
256+
} else {
257+
browser.pageAction.hide(tabId)
258+
}
259+
}
260+
261+
function onTabUpdated(tabId, changeInfo) {
262+
if (changeInfo.url) {
263+
switchPageAction(tabId, changeInfo.url)
264+
}
265+
}
266+
246267
function onTabRemoved(tabId, removeInfo) {
247268
state.tabBindings.delete(tabId)
248269
}
@@ -255,6 +276,11 @@ function onTabReplaced(addedTabId, removedTabId) {
255276
onTabRemoved(removedTabId, null)
256277
}
257278

279+
async function initTabs() {
280+
const tabs = await browser.tabs.query({})
281+
tabs.forEach(tab => switchPageAction(tab.id, tab.url))
282+
}
283+
258284
async function injectFrame(tab) {
259285
await browser.tabs.executeScript(tab.id, {file: POLYFILL_CONTENT_SCRIPT})
260286
await browser.tabs.executeScript(tab.id, {file: FRAME_INJECTOR_SCRIPT})
@@ -364,7 +390,11 @@ periodicDataRefresh()
364390

365391
browser.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ['*://*/*']}, ['blocking'])
366392
browser.webNavigation.onDOMContentLoaded.addListener(onDOMContentLoaded, {url: [{urlMatches: '.*'}]})
367-
browser.tabs.onRemoved.addListener(onTabRemoved)
368-
browser.tabs.onReplaced.addListener(onTabReplaced)
369393
browser.pageAction.onClicked.addListener(onIconClicked)
370394
browser.runtime.onMessage.addListener(onMessage)
395+
396+
browser.tabs.onRemoved.addListener(onTabRemoved)
397+
browser.tabs.onReplaced.addListener(onTabReplaced)
398+
browser.tabs.onUpdated.addListener(onTabUpdated)
399+
400+
initTabs().catch(e => console.log('Failed to init tabs', e))

0 commit comments

Comments
 (0)