/
aboutTor-content.js
157 lines (130 loc) · 4.99 KB
/
aboutTor-content.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
147
148
149
150
151
152
153
154
155
156
157
/*************************************************************************
* Copyright (c) 2017, The Tor Project, Inc.
* See LICENSE for licensing information.
*
* vim: set sw=2 sts=2 ts=8 et syntax=javascript:
*
* about:tor content script
*************************************************************************/
/*
* The following about:tor IPC messages are exchanged by this code and
* the code in torbutton.js:
* AboutTor:Loaded page loaded content -> chrome
* AboutTor:ChromeData privileged data chrome -> content
*/
var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
let { bindPrefAndInit, show_torbrowser_manual } = Cu.import("resource://torbutton/modules/utils.js", {});
var AboutTorListener = {
kAboutTorLoadedMessage: "AboutTor:Loaded",
kAboutTorChromeDataMessage: "AboutTor:ChromeData",
kAboutTorHideDonationBanner: "AboutTor:HideDonationBanner",
get isAboutTor() {
return content.document.documentURI.toLowerCase() == "about:tor";
},
init: function(aChromeGlobal) {
aChromeGlobal.addEventListener("AboutTorLoad", this, false, true);
},
handleEvent: function(aEvent) {
if (!this.isAboutTor)
return;
switch (aEvent.type) {
case "AboutTorLoad":
this.onPageLoad();
break;
case "pagehide":
this.onPageHide();
break;
}
},
receiveMessage: function(aMessage) {
if (!this.isAboutTor)
return;
switch (aMessage.name) {
case this.kAboutTorChromeDataMessage:
this.onChromeDataUpdate(aMessage.data);
break;
}
},
setupBannerClosing: function () {
let that = this;
let closer = content.document.getElementById("donation-banner-closer");
closer.addEventListener("click", function () {
sendAsyncMessage(that.kAboutTorHideDonationBanner);
});
let button = content.document.getElementById("donation-banner-button");
button.addEventListener("click", function () {
// Wait until page unloads so we don't hide banner before that.
content.addEventListener("unload", function () {
sendAsyncMessage(that.kAboutTorHideDonationBanner);
});
});
bindPrefAndInit("extensions.torbutton.donation_banner_countdown",
countdown => {
if (content.document && content.document.body) {
content.document.body.setAttribute(
"show-donation-banner", countdown > 0);
}
});
},
onPageLoad: function() {
// Arrange to update localized text and links.
bindPrefAndInit("intl.locale.requested", aNewVal => {
if (aNewVal !== null) {
this.onLocaleChange(aNewVal);
}
});
this.setupBannerClosing();
// Add message and event listeners.
addMessageListener(this.kAboutTorChromeDataMessage, this);
addEventListener("pagehide", this, false);
addEventListener("resize", this, false);
sendAsyncMessage(this.kAboutTorLoadedMessage);
},
onPageHide: function() {
removeEventListener("resize", this, false);
removeEventListener("pagehide", this, false);
removeMessageListener(this.kAboutTorChromeDataMessage, this);
},
onChromeDataUpdate: function(aData) {
let body = content.document.body;
// Update status: tor on/off, Tor Browser manual shown.
if (aData.torOn)
body.setAttribute("toron", "yes");
else
body.removeAttribute("toron");
if (show_torbrowser_manual())
body.setAttribute("showmanual", "yes");
else
body.removeAttribute("showmanual");
// Setting body.initialized="yes" displays the body.
body.setAttribute("initialized", "yes");
},
onLocaleChange: function(aLocale) {
// Set Tor Browser manual link.
content.document.getElementById("manualLink").href =
"https://tb-manual.torproject.org/" + aLocale;
// Don't use "Count Me In" phrase in non-en-US locales
if (!aLocale.startsWith("en")) {
let button = content.document.getElementById("donation-banner-button");
button.innerHTML = button.getAttribute("data-0");
let theURL = button.getAttribute("href");
button.setAttribute("href", theURL.slice(0,-2));
}
// Display the Tor Browser product name and version.
try {
const kBrandBundle = "chrome://branding/locale/brand.properties";
let brandBundle = Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Ci.nsIStringBundleService)
.createBundle(kBrandBundle);
let productName = brandBundle.GetStringFromName("brandFullName");
let tbbVersion = Services.prefs.getCharPref("torbrowser.version");
elem = content.document.getElementById("torstatus-version");
while (elem.firstChild)
elem.removeChild(elem.firstChild);
elem.appendChild(content.document.createTextNode(productName + '\n'
+ tbbVersion));
} catch (e) {}
}
};
AboutTorListener.init(this);