Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: popup.js

Issue 29532767: Issue 5593 - Use messaging in popup for prefs, whitelisting, and stats (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Remove odd use of promise rejection Created Sept. 19, 2017, 10:17 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « popup.html ('k') | skin/popup.css » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: popup.js
===================================================================
--- a/popup.js
+++ b/popup.js
@@ -16,54 +16,122 @@
*/
"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 checkPageReady(pageId, state)
+{
+ return new Promise(resolve =>
+ {
+ chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, ready =>
+ {
+ if (ready == state)
+ resolve();
+ });
+ });
+}
+
+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
Manish Jethani 2017/09/19 10:35:33 This comment seems unnecessary now as the code is
+ // already missed it.
+ checkPageReady(pageId, true)
+ ]);
+}
+
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) =>
+ checkPageReady(page.id, false).then(() =>
+ {
+ document.body.classList.add("nohtml");
+ whenPageReady(page.id).then(() =>
{
- 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 +147,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 +211,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);
« no previous file with comments | « popup.html ('k') | skin/popup.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld