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: Change whitelisting calls to use messaging Created Sept. 21, 2017, 7: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
« lib/whitelisting.js ('K') | « 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
@@ -14,56 +14,102 @@
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"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 tab = null;
Manish Jethani 2017/09/21 08:02:39 So now it's just a lightweight tab object and crea
+
+function getPref(key, callback)
Manish Jethani 2017/09/21 08:06:25 By the way, since we moved getPref and togglePref
Sebastian Noack 2017/09/21 22:57:17 Where accessing globals from other scripts cannot
Manish Jethani 2017/09/24 22:37:27 Done.
+{
+ chrome.runtime.sendMessage({type: "prefs.get", key}, callback);
+}
+
+function togglePref(key, callback)
+{
+ chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback);
+}
+
+function isPageWhitelisted(callback)
Manish Jethani 2017/09/21 08:02:39 I wonder if we should always use promises here.
+{
+ chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback);
+}
+
+function whenPageReady()
+{
+ let pageId = tab.id;
Sebastian Noack 2017/09/21 22:57:18 Why do we have to chache tab.id into a local varia
Manish Jethani 2017/09/24 22:37:27 It was not necessary, removed.
-let page = null;
+ return new Promise(resolve =>
+ {
+ let handlePageReady = () =>
Sebastian Noack 2017/09/21 22:57:17 I'm not sure if it's worth to move these two trivi
Manish Jethani 2017/09/24 22:37:26 Done.
+ {
+ ext.onMessage.removeListener(onMessage);
+ resolve();
+ };
+
+ let onMessage = (message, sender) =>
+ {
+ if (message.type == "composer.ready" && sender.page &&
+ sender.page.id == pageId)
+ {
+ handlePageReady();
+ }
+ };
+
+ ext.onMessage.addListener(onMessage);
+
+ chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, ready =>
+ {
+ if (ready)
+ handlePageReady();
+ });
+ });
+}
function onLoad()
{
- ext.pages.query({active: true, lastFocusedWindow: true}, pages =>
+ chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
{
- page = pages[0];
+ tab = tabs[0] && {id: tabs[0].id, url: tabs[0].url};
Manish Jethani 2017/09/21 08:02:37 I don't like to hold on to an object passed in by
Sebastian Noack 2017/09/21 22:57:18 Alternatively, you could use two global variables,
Manish Jethani 2017/09/24 22:37:27 We could do that, but we're using the tab object i
Sebastian Noack 2017/09/25 17:50:52 I see, I don't have a strong opinion then.
- // Mark page as 'local' or 'nohtml' to hide non-relevant elements
- if (!page || (page.url.protocol != "http:" &&
- page.url.protocol != "https:"))
+ let url = tab && tab.url && new URL(tab.url);
+
+ // Mark page as 'local' to hide non-relevant elements
+ if (!url || (url.protocol != "http:" && url.protocol != "https:"))
+ {
document.body.classList.add("local");
- else if (!require("filterComposer").isPageReady(page))
+ document.body.classList.remove("nohtml");
+ }
+ else
{
- document.body.classList.add("nohtml");
- require("messaging").getPort(window).on(
- "composer.ready", (message, sender) =>
- {
- if (sender.page.id == page.id)
- document.body.classList.remove("nohtml");
- }
- );
+ whenPageReady().then(() =>
+ {
+ 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 (tab)
{
- if (checkWhitelisted(page))
- document.body.classList.add("disabled");
+ isPageWhitelisted(whitelisted =>
+ {
+ if (whitelisted)
+ document.body.classList.add("disabled");
+ });
- page.sendMessage({type: "composer.content.getState"}, response =>
+ chrome.tabs.sendMessage(tab.id, {
+ type: "composer.content.getState"
+ },
+ response =>
{
if (response && response.active)
document.body.classList.add("clickhide-active");
});
}
});
document.getElementById("enabled").addEventListener(
@@ -79,74 +125,60 @@
{
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)
- {
- let host = getDecodedHostname(page.url).replace(/^www\./, "");
- let filter = Filter.fromText("@@||" + host + "^$document");
- if (filter.subscriptions.length && filter.disabled)
- filter.disabled = false;
- else
- {
- filter.disabled = false;
- FilterStorage.addFilter(filter);
- }
- }
- else
- {
- // Remove any exception rules applying to this URL
- let filter = checkWhitelisted(page);
- while (filter)
- {
- FilterStorage.removeFilter(filter);
- if (filter.subscriptions.length)
- filter.disabled = true;
- filter = checkWhitelisted(page);
- }
- }
+ chrome.runtime.sendMessage({
+ type: disabled ? "filters.whitelist" : "filters.unwhitelist",
+ tab
+ });
}
function activateClickHide()
{
document.body.classList.add("clickhide-active");
- page.sendMessage({type: "composer.content.startPickingElement"});
+ chrome.tabs.sendMessage(tab.id, {
Manish Jethani 2017/09/21 08:02:38 I forgot to account for this earlier. It needs to
+ type: "composer.content.startPickingElement"
+ });
// Close the popup after a few seconds, so user doesn't have to
activateClickHide.timeout = window.setTimeout(ext.closePopup, 5000);
}
function cancelClickHide()
{
if (activateClickHide.timeout)
{
window.clearTimeout(activateClickHide.timeout);
activateClickHide.timeout = null;
}
document.body.classList.remove("clickhide-active");
- page.sendMessage({type: "composer.content.finished"});
+ chrome.tabs.sendMessage(tab.id, {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);
« lib/whitelisting.js ('K') | « popup.html ('k') | skin/popup.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld