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

Powered by Google App Engine
This is Rietveld