Index: popup.js |
=================================================================== |
--- a/popup.js |
+++ b/popup.js |
@@ -16,54 +16,124 @@ |
*/ |
"use strict"; |
const {require} = ext.backgroundPage.getWindow(); |
const {Filter} = require("filterClasses"); |
const {FilterStorage} = require("filterStorage"); |
-const {Prefs} = require("prefs"); |
const {checkWhitelisted} = require("whitelisting"); |
const {getDecodedHostname} = require("url"); |
let page = null; |
+function createPageObject(tab) |
+{ |
+ if (!tab) |
+ return null; |
+ |
+ // Create a lightweight page object that resembles ext.Page, but with only |
+ // an id and an optional url property. |
+ let page = {id: tab.id}; |
+ |
+ if (tab.url) |
+ page.url = new URL(tab.url); |
+ |
+ return page; |
+} |
+ |
+function getPref(key, callback) |
+{ |
+ chrome.runtime.sendMessage({type: "prefs.get", key}, callback); |
+} |
+ |
+function togglePref(key, callback) |
+{ |
+ chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); |
+} |
+ |
+function assertPageReady(pageId) |
+{ |
+ return new Promise((resolve, reject) => |
+ { |
+ chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, ready => |
+ { |
+ if (ready) |
+ resolve(); |
+ else |
+ reject(); |
+ }); |
+ }); |
+} |
+ |
+function waitForPageReady(pageId) |
+{ |
+ return new Promise(resolve => |
+ { |
+ let listener = (message, sender) => |
+ { |
+ if (message.type == "composer.ready" && sender.page.id == pageId) |
+ { |
+ ext.onMessage.removeListener(listener); |
+ resolve(); |
+ } |
+ }; |
+ |
+ ext.onMessage.addListener(listener); |
+ }); |
+} |
+ |
+function whenPageReady(pageId) |
+{ |
+ return Promise.race([ |
+ waitForPageReady(pageId), |
+ // Check if the page is ready after adding the listener just in case we've |
+ // already missed it. |
+ new Promise(resolve => assertPageReady(pageId).then(resolve)) |
Wladimir Palant
2017/09/19 10:17:14
You cannot just ignore the rejection reason of a p
Manish Jethani
2017/09/19 10:22:50
I've removed this odd use of promise rejection now
|
+ ]); |
+} |
+ |
function onLoad() |
{ |
- ext.pages.query({active: true, lastFocusedWindow: true}, pages => |
+ chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => |
{ |
- page = pages[0]; |
+ page = createPageObject(tabs[0]); |
// Mark page as 'local' or 'nohtml' to hide non-relevant elements |
if (!page || (page.url.protocol != "http:" && |
page.url.protocol != "https:")) |
+ { |
document.body.classList.add("local"); |
- else if (!require("filterComposer").isPageReady(page)) |
+ } |
+ else |
{ |
- document.body.classList.add("nohtml"); |
- require("messaging").getPort(window).on( |
- "composer.ready", (message, sender) => |
+ assertPageReady(page.id).catch(() => |
+ { |
+ document.body.classList.add("nohtml"); |
+ whenPageReady(page.id).then(() => |
Wladimir Palant
2017/09/19 10:17:14
I think that "nohtml" class should just be there i
Manish Jethani
2017/09/19 10:22:50
Acknowledged.
I'll look into this next.
Manish Jethani
2017/09/19 10:35:32
Done.
|
{ |
- if (sender.page.id == page.id) |
- document.body.classList.remove("nohtml"); |
- } |
- ); |
+ document.body.classList.remove("nohtml"); |
+ }); |
+ }); |
} |
// Ask content script whether clickhide is active. If so, show |
// cancel button. If that isn't the case, ask background.html |
// whether it has cached filters. If so, ask the user whether she |
// wants those filters. Otherwise, we are in default state. |
if (page) |
{ |
if (checkWhitelisted(page)) |
document.body.classList.add("disabled"); |
- page.sendMessage({type: "composer.content.getState"}, response => |
+ chrome.tabs.sendMessage(page.id, { |
+ type: "composer.content.getState" |
+ }, |
+ response => |
{ |
if (response && response.active) |
document.body.classList.add("clickhide-active"); |
}); |
} |
}); |
document.getElementById("enabled").addEventListener( |
@@ -79,22 +149,25 @@ |
{ |
ext.showOptions(); |
}, false); |
// Set up collapsing of menu items |
for (let collapser of document.getElementsByClassName("collapse")) |
{ |
collapser.addEventListener("click", toggleCollapse, false); |
- if (!Prefs[collapser.dataset.option]) |
+ getPref(collapser.dataset.option, value => |
{ |
- document.getElementById( |
- collapser.dataset.collapsable |
- ).classList.add("collapsed"); |
- } |
+ if (value) |
+ { |
+ document.getElementById( |
+ collapser.dataset.collapsible |
+ ).classList.remove("collapsed"); |
+ } |
+ }); |
} |
} |
function toggleEnabled() |
{ |
let disabled = document.body.classList.toggle("disabled"); |
if (disabled) |
{ |
@@ -140,13 +213,14 @@ |
} |
document.body.classList.remove("clickhide-active"); |
page.sendMessage({type: "composer.content.finished"}); |
} |
function toggleCollapse(event) |
{ |
let collapser = event.currentTarget; |
- Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; |
- collapser.parentNode.classList.toggle("collapsed"); |
+ let collapsible = document.getElementById(collapser.dataset.collapsible); |
+ collapsible.classList.toggle("collapsed"); |
+ togglePref(collapser.dataset.option); |
} |
document.addEventListener("DOMContentLoaded", onLoad, false); |