-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
146 lines (131 loc) · 4.04 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-2016 Eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
(function(global)
{
global.E = function E(id)
{
return document.getElementById(id);
}
global.getDocLink = function(link, callback)
{
ext.backgroundPage.sendMessage({
type: "app.get",
what: "doclink",
link: link
}, callback);
}
global.checkShareResource = function(url, callback)
{
ext.backgroundPage.sendMessage(
{
type: "filters.blocked",
url: url,
requestType: "SCRIPT",
docDomain: "adblockplus.org",
thirdParty: true
}, callback);
}
global.openSharePopup = function(url)
{
var glassPane = E("glass-pane");
if (!glassPane)
{
glassPane = document.createElement("div");
glassPane.setAttribute("id", "glass-pane");
document.body.appendChild(glassPane);
}
var iframe = E("share-popup");
if (!iframe)
{
iframe = document.createElement("iframe");
iframe.setAttribute("id", "share-popup");
iframe.setAttribute("scrolling", "no");
glassPane.appendChild(iframe);
}
// Firefox 38+ no longer allows messaging using postMessage so we need
// to have a fake top level frame to avoid problems with scripts that try to
// communicate with the first-run page
var isGecko = ("Components" in window);
if (isGecko)
{
try
{
var Ci = Components.interfaces;
iframe.contentWindow
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDocShell)
.setIsBrowserInsideApp(Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID);
}
catch(ex)
{
console.error(ex);
}
}
var popupMessageReceived = false;
function resizePopup(width, height)
{
iframe.width = width;
iframe.height = height;
iframe.style.marginTop = -height / 2 + "px";
iframe.style.marginLeft = -width / 2 + "px";
popupMessageReceived = true;
window.removeEventListener("message", popupMessageListener);
}
var popupMessageListener = function(event)
{
if (!/[.\/]adblockplus\.org$/.test(event.origin)
|| !("width" in event.data)
|| !("height" in event.data))
return;
resizePopup(event.data.width, event.data.height);
};
// Firefox requires last parameter to be true to be triggered by
// unprivileged pages
window.addEventListener("message", popupMessageListener, false, true);
var popupLoadListener = function()
{
if (!popupMessageReceived && isGecko)
{
var rootElement = iframe.contentDocument.documentElement;
var width = rootElement.dataset.width;
var height = rootElement.dataset.height;
if (width && height)
resizePopup(width, height);
}
if (popupMessageReceived)
{
iframe.className = "visible";
var popupCloseListener = function()
{
iframe.className = glassPane.className = "";
document.removeEventListener("click", popupCloseListener);
};
document.addEventListener("click", popupCloseListener, false);
}
else
{
glassPane.className = "";
window.removeEventListener("message", popupMessageListener);
}
iframe.removeEventListener("load", popupLoadListener);
};
iframe.addEventListener("load", popupLoadListener, false);
iframe.src = url;
glassPane.className = "visible";
}
})(this);