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

Powered by Google App Engine
This is Rietveld