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

Side by Side Diff: common.js

Issue 29375899: Issue 4871 - Start using ESLint for adblockplusui (Closed)
Patch Set: Avoid violating operator-linebreak rule Created March 15, 2017, 4: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
« no previous file with comments | « background.js ('k') | devtools-panel.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
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 /* globals Components */
19
18 "use strict"; 20 "use strict";
19 21
20 (function(global) 22 function E(id)
21 { 23 {
22 global.E = function E(id) 24 return document.getElementById(id);
25 }
26
27 function getDocLink(link, callback)
28 {
29 ext.backgroundPage.sendMessage({
30 type: "app.get",
31 what: "doclink",
32 link
33 }, callback);
34 }
35
36 function checkShareResource(url, callback)
37 {
38 ext.backgroundPage.sendMessage({
39 type: "filters.blocked",
40 url,
41 requestType: "SCRIPT",
42 docDomain: "adblockplus.org",
43 thirdParty: true
44 }, callback);
45 }
46
47 function openSharePopup(url)
48 {
49 let glassPane = E("glass-pane");
50 if (!glassPane)
23 { 51 {
24 return document.getElementById(id); 52 glassPane = document.createElement("div");
53 glassPane.setAttribute("id", "glass-pane");
54 document.body.appendChild(glassPane);
25 } 55 }
26 56
27 global.getDocLink = function(link, callback) 57 let iframe = E("share-popup");
58 if (!iframe)
28 { 59 {
29 ext.backgroundPage.sendMessage({ 60 iframe = document.createElement("iframe");
30 type: "app.get", 61 iframe.setAttribute("id", "share-popup");
31 what: "doclink", 62 iframe.setAttribute("scrolling", "no");
32 link: link 63 glassPane.appendChild(iframe);
33 }, callback);
34 } 64 }
35 65
36 global.checkShareResource = function(url, callback) 66 // Firefox 38+ no longer allows messaging using postMessage so we need
67 // to have a fake top level frame to avoid problems with scripts that try to
68 // communicate with the first-run page
69 let isGecko = ("Components" in window);
70 if (isGecko)
37 { 71 {
38 ext.backgroundPage.sendMessage( 72 try
39 { 73 {
40 type: "filters.blocked", 74 let Ci = Components.interfaces;
41 url: url, 75 let docShell = iframe.contentWindow
42 requestType: "SCRIPT", 76 .QueryInterface(Ci.nsIInterfaceRequestor)
43 docDomain: "adblockplus.org", 77 .getInterface(Ci.nsIDocShell);
44 thirdParty: true 78
45 }, callback); 79 if (typeof docShell.frameType != "undefined")
80 {
81 // Gecko 47+
82 docShell.frameType = docShell.FRAME_TYPE_BROWSER;
83 }
84 else
85 {
86 // Legacy branch
87 docShell.setIsBrowserInsideApp(
88 Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID
89 );
90 }
91 }
92 catch (ex)
93 {
94 console.error(ex);
95 }
46 } 96 }
47 97
48 global.openSharePopup = function(url) 98 let popupMessageReceived = false;
99 function resizePopup(width, height)
49 { 100 {
50 var glassPane = E("glass-pane"); 101 iframe.width = width;
51 if (!glassPane) 102 iframe.height = height;
103 iframe.style.marginTop = -height / 2 + "px";
104 iframe.style.marginLeft = -width / 2 + "px";
105 popupMessageReceived = true;
106 window.removeEventListener("message", popupMessageListener);
107 }
108
109 let popupMessageListener = function(event)
110 {
111 if (!/[./]adblockplus\.org$/.test(event.origin) ||
112 !("width" in event.data) || !("height" in event.data))
113 return;
114
115 resizePopup(event.data.width, event.data.height);
116 };
117 // Firefox requires last parameter to be true to be triggered by
118 // unprivileged pages
119 window.addEventListener("message", popupMessageListener, false, true);
120
121 let popupLoadListener = function()
122 {
123 if (!popupMessageReceived && isGecko)
52 { 124 {
53 glassPane = document.createElement("div"); 125 let rootElement = iframe.contentDocument.documentElement;
54 glassPane.setAttribute("id", "glass-pane"); 126 let {width, height} = rootElement.dataset;
55 document.body.appendChild(glassPane); 127 if (width && height)
128 resizePopup(width, height);
56 } 129 }
57 130
58 var iframe = E("share-popup"); 131 if (popupMessageReceived)
59 if (!iframe)
60 { 132 {
61 iframe = document.createElement("iframe"); 133 iframe.className = "visible";
62 iframe.setAttribute("id", "share-popup"); 134
63 iframe.setAttribute("scrolling", "no"); 135 let popupCloseListener = function()
64 glassPane.appendChild(iframe); 136 {
137 iframe.className = glassPane.className = "";
138 document.removeEventListener("click", popupCloseListener);
139 };
140 document.addEventListener("click", popupCloseListener, false);
65 } 141 }
66 142 else
67 // Firefox 38+ no longer allows messaging using postMessage so we need
68 // to have a fake top level frame to avoid problems with scripts that try to
69 // communicate with the first-run page
70 var isGecko = ("Components" in window);
71 if (isGecko)
72 { 143 {
73 try 144 glassPane.className = "";
74 {
75 var Ci = Components.interfaces;
76 var docShell = iframe.contentWindow
77 .QueryInterface(Ci.nsIInterfaceRequestor)
78 .getInterface(Ci.nsIDocShell);
79
80 if (typeof docShell.frameType != "undefined")
81 {
82 // Gecko 47+
83 docShell.frameType = docShell.FRAME_TYPE_BROWSER;
84 }
85 else
86 {
87 // Legacy branch
88 docShell.setIsBrowserInsideApp(Ci.nsIScriptSecurityManager.UNKNOWN_APP _ID);
89 }
90 }
91 catch(ex)
92 {
93 console.error(ex);
94 }
95 }
96
97 var popupMessageReceived = false;
98 function resizePopup(width, height)
99 {
100 iframe.width = width;
101 iframe.height = height;
102 iframe.style.marginTop = -height / 2 + "px";
103 iframe.style.marginLeft = -width / 2 + "px";
104 popupMessageReceived = true;
105 window.removeEventListener("message", popupMessageListener); 145 window.removeEventListener("message", popupMessageListener);
106 } 146 }
107 147
108 var popupMessageListener = function(event) 148 iframe.removeEventListener("load", popupLoadListener);
109 { 149 };
110 if (!/[.\/]adblockplus\.org$/.test(event.origin) 150 iframe.addEventListener("load", popupLoadListener, false);
111 || !("width" in event.data)
112 || !("height" in event.data))
113 return;
114 151
115 resizePopup(event.data.width, event.data.height); 152 iframe.src = url;
116 }; 153 glassPane.className = "visible";
117 // Firefox requires last parameter to be true to be triggered by 154 }
118 // unprivileged pages
119 window.addEventListener("message", popupMessageListener, false, true);
120
121 var popupLoadListener = function()
122 {
123 if (!popupMessageReceived && isGecko)
124 {
125 var rootElement = iframe.contentDocument.documentElement;
126 var width = rootElement.dataset.width;
127 var height = rootElement.dataset.height;
128 if (width && height)
129 resizePopup(width, height);
130 }
131
132 if (popupMessageReceived)
133 {
134 iframe.className = "visible";
135
136 var popupCloseListener = function()
137 {
138 iframe.className = glassPane.className = "";
139 document.removeEventListener("click", popupCloseListener);
140 };
141 document.addEventListener("click", popupCloseListener, false);
142 }
143 else
144 {
145 glassPane.className = "";
146 window.removeEventListener("message", popupMessageListener);
147 }
148
149 iframe.removeEventListener("load", popupLoadListener);
150 };
151 iframe.addEventListener("load", popupLoadListener, false);
152
153 iframe.src = url;
154 glassPane.className = "visible";
155 }
156 })(this);
OLDNEW
« no previous file with comments | « background.js ('k') | devtools-panel.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld