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

Delta Between Two Patch Sets: safari/ext/content.js

Issue 29340571: Issue 3687 - Add experimental support for Safari content blockers (Closed)
Left Patch Set: Created April 19, 2016, 1:59 p.m.
Right Patch Set: Addressed Nits Created May 18, 2016, 11:30 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « safari/ext/background.js ('k') | safari/include.youtube.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 14 matching lines...) Expand all
25 25
26 /* Intialization */ 26 /* Intialization */
27 27
28 var beforeLoadEvent = document.createEvent("Event"); 28 var beforeLoadEvent = document.createEvent("Event");
29 beforeLoadEvent.initEvent("beforeload"); 29 beforeLoadEvent.initEvent("beforeload");
30 30
31 // Decide if we should use the new content blocker API or not. (Note when the 31 // Decide if we should use the new content blocker API or not. (Note when the
32 // API is used Safari breaks the canLoad function, making it either throw an 32 // API is used Safari breaks the canLoad function, making it either throw an
33 // exception or return true when used.) 33 // exception or return true when used.)
34 var usingContentBlockerAPI = true; 34 var usingContentBlockerAPI = true;
35 try { 35 try
Sebastian Noack 2016/05/12 11:12:23 Nit: Open brace on newline.
kzar 2016/05/17 15:15:40 Done.
36 {
36 if (safari.self.tab.canLoad(beforeLoadEvent, 37 if (safari.self.tab.canLoad(beforeLoadEvent,
37 {category: "usingContentBlockerAPI"}) != true) 38 {category: "request",
39 payload: {type: "prefs.get",
40 key: "safariContentBlocker"}}) != tru e)
38 usingContentBlockerAPI = false; 41 usingContentBlockerAPI = false;
39 } 42 }
40 catch (e) { }; 43 catch (e)
44 {
45 }
41 46
42 var isTopLevel; 47 var isTopLevel;
43 var isPrerendered; 48 var isPrerendered;
44 var documentId; 49 var documentId;
45 function notifyFrameLoading() 50 function notifyFrameLoading()
46 { 51 {
47 isTopLevel = window == window.top; 52 isTopLevel = window == window.top;
48 isPrerendered = document.visibilityState == "prerender"; 53 isPrerendered = document.visibilityState == "prerender";
49 documentId = Math.random().toString().substr(2); 54 documentId = Math.random().toString().substr(2);
50 55
51 // Notify the background page that this frame is loading, generating 56 // Notify the background page that this frame is loading, generating
52 // ourselves a random documentId while we're at it. That way the background 57 // ourselves a random documentId while we're at it. That way the background
53 // page can communicate with us reliably, despite limitations in Safari's 58 // page can communicate with us reliably, despite limitations in Safari's
54 // extension API. 59 // extension API.
55 safari.self.tab.dispatchMessage("loading", { 60 safari.self.tab.dispatchMessage("loading", {
56 url: window.location.href, 61 url: window.location.href,
57 referrer: document.referrer, 62 referrer: document.referrer,
58 isTopLevel: isTopLevel, 63 isTopLevel: isTopLevel,
59 isPrerendered: isPrerendered, 64 isPrerendered: isPrerendered,
60 documentId: documentId 65 documentId: documentId,
66 legacyAPISupported: "canLoad" in safari.self.tab &&
67 "onbeforeload" in Element.prototype
61 }); 68 });
62 } 69 }
63 70
64 // We must notify the background page when this page is first loadeding (now) 71 // We must notify the background page when this page is first loadeding (now)
65 // but also when it is re-shown (if the user uses the back button to return to 72 // but also when it is re-shown (if the user uses the back button to return to
66 // this page in the future). 73 // this page in the future).
67 notifyFrameLoading(); 74 notifyFrameLoading();
68 window.addEventListener("pageshow", function(event) 75 window.addEventListener("pageshow", function(event)
69 { 76 {
70 if (event.persisted) 77 if (event.persisted)
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 // been prevented from loading by having their "beforeload" event 148 // been prevented from loading by having their "beforeload" event
142 // cancelled. That is a "load" event for blocked frames, and an "error" 149 // cancelled. That is a "load" event for blocked frames, and an "error"
143 // event for other blocked elements. We need to dispatch those events 150 // event for other blocked elements. We need to dispatch those events
144 // manually here to avoid breaking element collapsing and pages that 151 // manually here to avoid breaking element collapsing and pages that
145 // rely on those events. 152 // rely on those events.
146 setTimeout(function() 153 setTimeout(function()
147 { 154 {
148 var evt = document.createEvent("Event"); 155 var evt = document.createEvent("Event");
149 evt.initEvent(eventName); 156 evt.initEvent(eventName);
150 event.target.dispatchEvent(evt); 157 event.target.dispatchEvent(evt);
151 }, 0); 158 });
152 } 159 }
153 }, true); 160 }, true);
154 } 161 }
155 162
156 163
157 /* Context menus */ 164 /* Context menus */
158 165
159 document.addEventListener("contextmenu", function(event) 166 document.addEventListener("contextmenu", function(event)
160 { 167 {
161 var element = event.srcElement; 168 var element = event.srcElement;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 }); 223 });
217 224
218 225
219 /* Detecting extension reload/disable/uninstall (not supported on Safari) */ 226 /* Detecting extension reload/disable/uninstall (not supported on Safari) */
220 227
221 ext.onExtensionUnloaded = { 228 ext.onExtensionUnloaded = {
222 addListener: function() {}, 229 addListener: function() {},
223 removeListener: function() {} 230 removeListener: function() {}
224 }; 231 };
225 })(); 232 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld