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

Powered by Google App Engine
This is Rietveld