Skip to content

Commit

Permalink
Switch to pageAction (attempt N2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsavenko committed Aug 10, 2018
1 parent a91d401 commit b6859ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
archive.zip
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

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

"icons": {
"128": "res/icon.png"
},

"page_action": {
"default_icon": "res/icon.png",
"show_matches": ["*://*/*"]
"default_title": "Moy.Design"
},

"background": {
Expand Down
34 changes: 32 additions & 2 deletions src/bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const TEMPLATES_DIR = 'MoyTemplates'
const MEDIA_DOMAINS = ['youtube.com', 'youtu.be', 'ytimg.com', 'googlevideo.com', 'vimeo.com', 'vimeocdn.com',
'lj-toys.com', '9cache.com']
const ORIGINAL_LOOK_NAME = 'Original look'
const PAGE_ACTION_URL_PROTOCOLS = ['http:', 'https:']
const PAGE_ACTION_BANNED_URLS = ['https://addons.mozilla.org', 'https://chrome.google.com/webstore']

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

function switchPageAction(tabId, url) {
if (0 > tabId && !url) {
return
}
const isProtocolOk = PAGE_ACTION_URL_PROTOCOLS.includes(new URL(url).protocol)
const isUrlBanned = PAGE_ACTION_BANNED_URLS.find(u => url.startsWith(u))
if (isProtocolOk && !isUrlBanned) {
browser.pageAction.show(tabId)
} else {
browser.pageAction.hide(tabId)
}
}

function onTabUpdated(tabId, changeInfo) {
if (changeInfo.url) {
switchPageAction(tabId, changeInfo.url)
}
}

function onTabRemoved(tabId, removeInfo) {
state.tabBindings.delete(tabId)
}
Expand All @@ -255,6 +276,11 @@ function onTabReplaced(addedTabId, removedTabId) {
onTabRemoved(removedTabId, null)
}

async function initTabs() {
const tabs = await browser.tabs.query({})
tabs.forEach(tab => switchPageAction(tab.id, tab.url))
}

async function injectFrame(tab) {
await browser.tabs.executeScript(tab.id, {file: POLYFILL_CONTENT_SCRIPT})
await browser.tabs.executeScript(tab.id, {file: FRAME_INJECTOR_SCRIPT})
Expand Down Expand Up @@ -364,7 +390,11 @@ periodicDataRefresh()

browser.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ['*://*/*']}, ['blocking'])
browser.webNavigation.onDOMContentLoaded.addListener(onDOMContentLoaded, {url: [{urlMatches: '.*'}]})
browser.tabs.onRemoved.addListener(onTabRemoved)
browser.tabs.onReplaced.addListener(onTabReplaced)
browser.pageAction.onClicked.addListener(onIconClicked)
browser.runtime.onMessage.addListener(onMessage)

browser.tabs.onRemoved.addListener(onTabRemoved)
browser.tabs.onReplaced.addListener(onTabReplaced)
browser.tabs.onUpdated.addListener(onTabUpdated)

initTabs().catch(e => console.log('Failed to init tabs', e))

0 comments on commit b6859ac

Please sign in to comment.