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

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

Issue 29340571: Issue 3687 - Add experimental support for Safari content blockers (Closed)
Patch Set: Remove onChange logic for checkboxes Created May 18, 2016, 10:43 a.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
(...skipping 10 matching lines...) Expand all
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 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
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;
35 try
36 {
37 if (safari.self.tab.canLoad(beforeLoadEvent,
38 {category: "request",
39 payload: {type: "prefs.get",
40 key: "safariContentBlocker"}}) != tru e)
41 usingContentBlockerAPI = false;
42 }
43 catch (e)
44 { }
Sebastian Noack 2016/05/18 11:16:33 Nit: The closing brace should go on a new line as
kzar 2016/05/18 11:32:08 Done.
45
31 var isTopLevel; 46 var isTopLevel;
32 var isPrerendered; 47 var isPrerendered;
33 var documentId; 48 var documentId;
34 function notifyFrameLoading() 49 function notifyFrameLoading()
35 { 50 {
36 isTopLevel = window == window.top; 51 isTopLevel = window == window.top;
37 isPrerendered = document.visibilityState == "prerender"; 52 isPrerendered = document.visibilityState == "prerender";
38 documentId = Math.random().toString().substr(2); 53 documentId = Math.random().toString().substr(2);
39 54
40 // Notify the background page that this frame is loading, generating 55 // Notify the background page that this frame is loading, generating
41 // ourselves a random documentId while we're at it. That way the background 56 // ourselves a random documentId while we're at it. That way the background
42 // page can communicate with us reliably, despite limitations in Safari's 57 // page can communicate with us reliably, despite limitations in Safari's
43 // extension API. 58 // extension API.
44 safari.self.tab.dispatchMessage("loading", { 59 safari.self.tab.dispatchMessage("loading", {
45 url: window.location.href, 60 url: window.location.href,
46 referrer: document.referrer, 61 referrer: document.referrer,
47 isTopLevel: isTopLevel, 62 isTopLevel: isTopLevel,
48 isPrerendered: isPrerendered, 63 isPrerendered: isPrerendered,
49 documentId: documentId 64 documentId: documentId,
65 legacyAPISupported: "canLoad" in safari.self.tab &&
66 "onbeforeload" in Element.prototype
50 }); 67 });
51 } 68 }
52 69
53 // We must notify the background page when this page is first loadeding (now) 70 // We must notify the background page when this page is first loadeding (now)
54 // but also when it is re-shown (if the user uses the back button to return to 71 // but also when it is re-shown (if the user uses the back button to return to
55 // this page in the future). 72 // this page in the future).
56 notifyFrameLoading(); 73 notifyFrameLoading();
57 window.addEventListener("pageshow", function(event) 74 window.addEventListener("pageshow", function(event)
58 { 75 {
59 if (event.persisted) 76 if (event.persisted)
60 notifyFrameLoading(); 77 notifyFrameLoading();
61 }); 78 });
62 79
63 // Notify the background page when a prerendered page is displayed. That way 80 if (!usingContentBlockerAPI)
64 // the existing page of the tab can be replaced with this new one.
65 if (isTopLevel && isPrerendered)
66 { 81 {
67 var onVisibilitychange = function() 82 // Notify the background page when a prerendered page is displayed. That way
83 // the existing page of the tab can be replaced with this new one.
84 if (isTopLevel && isPrerendered)
68 { 85 {
69 safari.self.tab.dispatchMessage("replaced", {documentId: documentId}); 86 var onVisibilitychange = function()
70 document.removeEventListener("visibilitychange", onVisibilitychange); 87 {
71 }; 88 safari.self.tab.dispatchMessage("replaced", {documentId: documentId});
72 document.addEventListener("visibilitychange", onVisibilitychange); 89 document.removeEventListener("visibilitychange", onVisibilitychange);
90 };
91 document.addEventListener("visibilitychange", onVisibilitychange);
92 }
93
94 /* Web requests */
95
96 document.addEventListener("beforeload", function(event)
97 {
98 // we don't block non-HTTP requests anyway, so we can bail out
99 // without asking the background page. This is even necessary
100 // because passing large data (like a photo encoded as data: URL)
101 // to the background page, freezes Safari.
102 if (/^(?!https?:)[\w-]+:/.test(event.url))
103 return;
104
105 var type = "OTHER";
106 var eventName = "error";
107
108 switch(event.target.localName)
109 {
110 case "frame":
111 case "iframe":
112 type = "SUBDOCUMENT";
113 eventName = "load";
114 break;
115 case "img":
116 case "input":
117 type = "IMAGE";
118 break;
119 case "video":
120 case "audio":
121 case "source":
122 type = "MEDIA";
123 break;
124 case "object":
125 case "embed":
126 type = "OBJECT";
127 break;
128 case "script":
129 type = "SCRIPT";
130 break;
131 case "link":
132 if (/\bstylesheet\b/i.test(event.target.rel))
133 type = "STYLESHEET";
134 break;
135 }
136
137 if (!safari.self.tab.canLoad(
138 event, {
139 category: "webRequest",
140 url: event.url,
141 type: type,
142 documentId: documentId}))
143 {
144 event.preventDefault();
145
146 // Safari doesn't dispatch the expected events for elements that have
147 // been prevented from loading by having their "beforeload" event
148 // cancelled. That is a "load" event for blocked frames, and an "error"
149 // event for other blocked elements. We need to dispatch those events
150 // manually here to avoid breaking element collapsing and pages that
151 // rely on those events.
152 setTimeout(function()
153 {
154 var evt = document.createEvent("Event");
155 evt.initEvent(eventName);
156 event.target.dispatchEvent(evt);
157 }, 0);
Sebastian Noack 2016/05/18 11:16:33 Nit: 0 is the default for the second argument to s
kzar 2016/05/18 11:32:08 Done.
158 }
159 }, true);
73 } 160 }
74 161
75 162
76 /* Web requests */
77
78 document.addEventListener("beforeload", function(event)
79 {
80 // we don't block non-HTTP requests anyway, so we can bail out
81 // without asking the background page. This is even necessary
82 // because passing large data (like a photo encoded as data: URL)
83 // to the background page, freezes Safari.
84 if (/^(?!https?:)[\w-]+:/.test(event.url))
85 return;
86
87 var type = "OTHER";
88 var eventName = "error";
89
90 switch(event.target.localName)
91 {
92 case "frame":
93 case "iframe":
94 type = "SUBDOCUMENT";
95 eventName = "load";
96 break;
97 case "img":
98 case "input":
99 type = "IMAGE";
100 break;
101 case "video":
102 case "audio":
103 case "source":
104 type = "MEDIA";
105 break;
106 case "object":
107 case "embed":
108 type = "OBJECT";
109 break;
110 case "script":
111 type = "SCRIPT";
112 break;
113 case "link":
114 if (/\bstylesheet\b/i.test(event.target.rel))
115 type = "STYLESHEET";
116 break;
117 }
118
119 if (!safari.self.tab.canLoad(event, {
120 category: "webRequest",
121 url: event.url,
122 type: type,
123 documentId: documentId}))
124 {
125 event.preventDefault();
126
127 // Safari doesn't dispatch the expected events for elements that have been
128 // prevented from loading by having their "beforeload" event cancelled.
129 // That is a "load" event for blocked frames, and an "error" event for
130 // other blocked elements. We need to dispatch those events manually here
131 // to avoid breaking element collapsing and pages that rely on those event s.
132 setTimeout(function()
133 {
134 var evt = document.createEvent("Event");
135 evt.initEvent(eventName);
136 event.target.dispatchEvent(evt);
137 }, 0);
138 }
139 }, true);
140
141
142 /* Context menus */ 163 /* Context menus */
143 164
144 document.addEventListener("contextmenu", function(event) 165 document.addEventListener("contextmenu", function(event)
145 { 166 {
146 var element = event.srcElement; 167 var element = event.srcElement;
147 safari.self.tab.setContextMenuEventUserInfo(event, { 168 safari.self.tab.setContextMenuEventUserInfo(event, {
148 documentId: documentId, 169 documentId: documentId,
149 tagName: element.localName 170 tagName: element.localName
150 }); 171 });
151 }); 172 });
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 }); 222 });
202 223
203 224
204 /* Detecting extension reload/disable/uninstall (not supported on Safari) */ 225 /* Detecting extension reload/disable/uninstall (not supported on Safari) */
205 226
206 ext.onExtensionUnloaded = { 227 ext.onExtensionUnloaded = {
207 addListener: function() {}, 228 addListener: function() {},
208 removeListener: function() {} 229 removeListener: function() {}
209 }; 230 };
210 })(); 231 })();
OLDNEW

Powered by Google App Engine
This is Rietveld