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: Check for page ready again after adding listener Created Sept. 19, 2017, 8:54 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,120 @@
*/
"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, callback)
+{
+ chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, callback);
+}
+
+function whenPageReady(pageId, callback)
+{
+ let handlePageReady = () =>
+ {
+ handlePageReady = null;
Manish Jethani 2017/09/19 08:59:13 Set this to null so it's never called again.
+ ext.onMessage.removeListener(listener);
+ callback();
+ };
+
+ let listener = (message, sender) =>
+ {
+ if (handlePageReady && message.type == "composer.ready" &&
+ sender.page.id == pageId)
+ {
+ handlePageReady();
+ }
+ };
+
+ ext.onMessage.addListener(listener);
+
+ // Check if the page is ready after adding the listener just in case we've
+ // already missed it.
+ checkPageReady(pageId, ready =>
+ {
+ if (ready && handlePageReady)
+ handlePageReady();
+ });
+}
+
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, ready =>
Manish Jethani 2017/09/19 08:59:13 We still do this check since it's going to return
+ {
+ if (!ready)
{
- if (sender.page.id == page.id)
+ document.body.classList.add("nohtml");
+ whenPageReady(page.id, () =>
+ {
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 +145,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 +209,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