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

Side by Side Diff: safari/ext/content.js

Issue 29829615: Issue 6786 - Switch Safari 12+ users to content blocking API (Closed)
Patch Set: Created July 13, 2018, 6:18 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
« options.js ('K') | « safari/contentBlocking.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 (function() 18 (function()
19 { 19 {
20 // the safari object is missing in frames created from javascript: URLs. 20 // the safari object is missing in frames created from javascript: URLs.
21 // So we have to fallback to the safari object from the parent frame. 21 // So we have to fallback to the safari object from the parent frame.
22 if (!("safari" in window)) 22 if (!("safari" in window))
23 window.safari = window.parent.safari; 23 window.safari = window.parent.safari;
24 24
25 25
26 /* Intialization */ 26 /* Intialization */
27 27
28 var beforeLoadEvent = document.createEvent("Event"); 28 var applicatonVersion = navigator.userAgent.match(/Version\/([\d.]+)/)[1];
29 beforeLoadEvent.initEvent("beforeload", false, true); 29 var majorApplicationVersion = parseInt(applicatonVersion.split(".")[0], 10);
30 30
31 // Decide if we should use the new content blocker API or not. (Note when the 31 var beforeLoadEvent;
32 // API is used Safari breaks the canLoad function, making it either throw an
33 // exception or return true when used.)
34 var usingContentBlockerAPI = true; 32 var usingContentBlockerAPI = true;
35 try 33
34 // Safari 12 automatically disables extensions which use the old canLoad API,
35 // so avoid using the old APIs on Safari 12!
36 if (majorApplicationVersion < 12)
36 { 37 {
37 if (safari.self.tab.canLoad(beforeLoadEvent, 38 beforeLoadEvent = document.createEvent("Event");
38 {category: "request", 39 beforeLoadEvent.initEvent("beforeload", false, true);
39 payload: {type: "prefs.get", 40
40 key: "safariContentBlocker"}}) != tru e) 41 // Decide if we should use the new content blocker API or not. (Note when th e
41 usingContentBlockerAPI = false; 42 // API is used Safari breaks the canLoad function, making it either throw an
42 } 43 // exception or return true when used.)
43 catch (e) 44 try
44 { 45 {
46 if (safari.self.tab.canLoad(beforeLoadEvent,
47 {category: "request",
48 payload: {type: "prefs.get",
49 key: "safariContentBlocker"}}) != t rue)
50 usingContentBlockerAPI = false;
51 }
52 catch (e)
53 {
54 }
45 } 55 }
46 56
47 var isTopLevel; 57 var isTopLevel;
48 var isPrerendered; 58 var isPrerendered;
49 var documentId; 59 var documentId;
50 function notifyFrameLoading() 60 function notifyFrameLoading()
51 { 61 {
52 isTopLevel = window == window.top; 62 isTopLevel = window == window.top;
53 isPrerendered = document.visibilityState == "prerender"; 63 isPrerendered = document.visibilityState == "prerender";
54 documentId = Math.random().toString().substr(2); 64 documentId = Math.random().toString().substr(2);
55 65
56 // Notify the background page that this frame is loading, generating 66 // Notify the background page that this frame is loading, generating
57 // ourselves a random documentId while we're at it. That way the background 67 // ourselves a random documentId while we're at it. That way the background
58 // page can communicate with us reliably, despite limitations in Safari's 68 // page can communicate with us reliably, despite limitations in Safari's
59 // extension API. 69 // extension API.
60 safari.self.tab.dispatchMessage("loading", { 70 safari.self.tab.dispatchMessage("loading", {
61 url: window.location.href, 71 url: window.location.href,
62 referrer: document.referrer, 72 referrer: document.referrer,
63 isTopLevel: isTopLevel, 73 isTopLevel: isTopLevel,
64 isPrerendered: isPrerendered, 74 isPrerendered: isPrerendered,
65 documentId: documentId, 75 documentId: documentId,
66 legacyAPISupported: "canLoad" in safari.self.tab && 76 legacyAPISupported: majorApplicationVersion < 12 &&
77 "canLoad" in safari.self.tab &&
67 "onbeforeload" in Element.prototype 78 "onbeforeload" in Element.prototype
68 }); 79 });
69 } 80 }
70 81
71 // We must notify the background page when this page is first loadeding (now) 82 // We must notify the background page when this page is first loadeding (now)
72 // but also when it is re-shown (if the user uses the back button to return to 83 // but also when it is re-shown (if the user uses the back button to return to
73 // this page in the future). 84 // this page in the future).
74 notifyFrameLoading(); 85 notifyFrameLoading();
75 window.addEventListener("pageshow", function(event) 86 window.addEventListener("pageshow", function(event)
76 { 87 {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 /* Background page */ 187 /* Background page */
177 188
178 ext.backgroundPage = { 189 ext.backgroundPage = {
179 sendMessage: function(message, responseCallback) 190 sendMessage: function(message, responseCallback)
180 { 191 {
181 messageProxy.sendMessage(message, responseCallback, 192 messageProxy.sendMessage(message, responseCallback,
182 {documentId: documentId}); 193 {documentId: documentId});
183 }, 194 },
184 sendMessageSync: function(message) 195 sendMessageSync: function(message)
185 { 196 {
186 return safari.self.tab.canLoad( 197 if (majorApplicationVersion < 12)
187 beforeLoadEvent, 198 {
188 { 199 return safari.self.tab.canLoad(
189 category: "request", 200 beforeLoadEvent,
190 documentId: documentId, 201 {
191 payload: message 202 category: "request",
192 } 203 documentId: documentId,
193 ); 204 payload: message
205 }
206 );
207 }
194 } 208 }
195 }; 209 };
196 210
197 211
198 /* Message processing */ 212 /* Message processing */
199 213
200 var messageProxy = new ext._MessageProxy(safari.self.tab); 214 var messageProxy = new ext._MessageProxy(safari.self.tab);
201 215
202 safari.self.addEventListener("message", function(event) 216 safari.self.addEventListener("message", function(event)
203 { 217 {
(...skipping 19 matching lines...) Expand all
223 }); 237 });
224 238
225 239
226 /* Detecting extension reload/disable/uninstall (not supported on Safari) */ 240 /* Detecting extension reload/disable/uninstall (not supported on Safari) */
227 241
228 ext.onExtensionUnloaded = { 242 ext.onExtensionUnloaded = {
229 addListener: function() {}, 243 addListener: function() {},
230 removeListener: function() {} 244 removeListener: function() {}
231 }; 245 };
232 })(); 246 })();
OLDNEW
« options.js ('K') | « safari/contentBlocking.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld