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

Side by Side Diff: lib/elemHideFF.js

Issue 29346613: Issue 521 - Inject our stylesheet on per-site basis rather than globally (Closed)
Patch Set: Created June 17, 2016, 2:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 let {port} = require("messaging"); 20 let {port} = require("messaging");
21 let {ElemHide} = require("elemHide"); 21 let {ElemHide} = require("elemHide");
22 let {FilterNotifier} = require("filterNotifier"); 22 let {FilterNotifier} = require("filterNotifier");
23 let {FilterStorage} = require("filterStorage");
23 let {Prefs} = require("prefs"); 24 let {Prefs} = require("prefs");
24 let {Utils} = require("utils"); 25 let {Policy} = require("contentPolicy");
25 26
26 /** 27 FilterNotifier.on("elemhideupdate", () => port.emit("elemhideupdate"));
27 * Indicates whether the element hiding stylesheet is currently applied.
28 * @type Boolean
29 */
30 let applied = false;
31 28
32 /** 29 port.on("getSelectors", () => ElemHide.getSelectors());
33 * Stylesheet URL to be registered
34 * @type nsIURI
35 */
36 let styleURL = Utils.makeURI("about:abp-elemhide?css");
37 30
38 function init() 31 port.on("elemhideEnabled", ({frames, isPrivate}) =>
39 { 32 {
40 port.on("getSelectors", () => ElemHide.getSelectors()); 33 if (!Prefs.enabled)
34 return {enabled: false};
41 35
42 apply(); 36 let hit = Policy.isFrameWhitelisted(frames, true);
43 onShutdown.add(unapply); 37 if (hit)
44
45 Prefs.addListener(function(name)
46 { 38 {
47 if (name == "enabled") 39 let [frameIndex, contentType, docDomain, thirdParty, location, filter] = hit ;
48 apply(); 40 if (!isPrivate)
49 }); 41 FilterStorage.increaseHitCount(filter);
50 42 return {
51 FilterNotifier.on("elemhideupdate", onUpdate); 43 enabled: false,
52 } 44 contentType, docDomain, thirdParty, location,
53 45 filter: filter.text, filterType: filter.type
54 function onUpdate() 46 };
55 {
56 // Call apply() asynchronously and protect against reentrance - multiple
57 // change events shouldn't result in multiple calls.
58 if (onUpdate.inProgress)
59 return;
60
61 onUpdate.inProgress = true;
62 Utils.runAsync(() =>
63 {
64 onUpdate.inProgress = false;
65 apply();
66 });
67 }
68
69 function apply()
70 {
71 unapply();
72
73 if (!Prefs.enabled)
74 return;
75
76 try
77 {
78 Utils.styleService.loadAndRegisterSheet(styleURL,
79 Ci.nsIStyleSheetService.USER_SHEET);
80 applied = true;
81 } 47 }
82 catch (e) 48 else
83 { 49 return {enabled: true};
84 Cu.reportError(e); 50 });
85 }
86 }
87
88 function unapply()
89 {
90 if (applied)
91 {
92 try
93 {
94 Utils.styleService.unregisterSheet(styleURL,
95 Ci.nsIStyleSheetService.USER_SHEET);
96 }
97 catch (e)
98 {
99 Cu.reportError(e);
100 }
101 applied = false;
102 }
103 }
104
105 // Send dummy message before initializing, this delay makes sure that the child
106 // modules are loaded and our protocol handler registered.
107 port.emitWithResponse("ping").then(init);
OLDNEW
« lib/child/elemHide.js ('K') | « lib/child/main.js ('k') | lib/main.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld