Skip to content

Commit

Permalink
Bug 23483: Donation banner on about:tor for 2017
Browse files Browse the repository at this point in the history
(Also removes a dot from aboutTor.donationBanner.slogan)
  • Loading branch information
arthuredelstein committed Sep 21, 2017
1 parent 0387a6f commit b3ff986
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/chrome/content/aboutTor/aboutTor-content.js
Expand Up @@ -105,6 +105,11 @@ var AboutTorListener = {
else
body.removeAttribute("showmanual");

if (aData.bannerData)
body.setAttribute("banner-data", aData.bannerData);
else
body.removeAttribute("banner-data");

// Setting body.initialized="yes" displays the body, which must be done
// at this point because our remaining initialization depends on elements
// being visible so that their size and position are accurate.
Expand Down
19 changes: 18 additions & 1 deletion src/chrome/content/aboutTor/aboutTor.xhtml
Expand Up @@ -21,6 +21,8 @@
<title>&aboutTor.title;</title>
<link rel="stylesheet" type="text/css" media="all"
href="resource://torbutton/chrome/skin/aboutTor.css"/>
<link rel="stylesheet" type="text/css" media="all"
href="resource://torbutton/chrome/skin/donation_banner.css"/>
<script type="text/javascript;version=1.7">
<![CDATA[
window.addEventListener("pageshow", function() {
Expand All @@ -31,6 +33,21 @@ window.addEventListener("pageshow", function() {
</script>
</head>
<body dir="&locale.dir;">
<div id="banner">
<div id="banner-contents-container">
<div id="banner-tagline"><span></span></div>
<div id="banner-slogan"><span></span></div>
<a id="banner-donate-button-link"
href="https://www.torproject.org/donate/donate-tbb">
<div id="banner-donate-button">
<div id="banner-donate-button-inner">
<span></span>
</div>
</div>
</a>
</div>
</div>
<div id="banner-spacer"></div>
<div id="torstatus" class="top">
<div id="torstatus-version"/>
<div id="torstatus-image"/>
Expand Down Expand Up @@ -112,6 +129,6 @@ window.addEventListener("pageshow", function() {
<p>&aboutTor.footer.label;
<a href="&aboutTor.learnMore.link;">&aboutTor.learnMore.label;</a></p>
</div>

<script src="resource://torbutton/chrome/content/aboutTor/donation_banner.js"></script>
</body>
</html>
105 changes: 105 additions & 0 deletions src/chrome/content/aboutTor/donation_banner.js
@@ -0,0 +1,105 @@
/* jshint esnext:true */

let sel = selector => document.querySelector(selector);

// Shrink the font size if the text in the given element is overflowing.
let fitTextInElement = function (element) {
element.style.fontSize = "8px";
let defaultWidth = element.scrollWidth,
defaultHeight = element.scrollHeight;
let bestSize;
for (let testSize = 8; testSize <= 40; testSize += 0.5) {
element.style.fontSize = `${testSize}px`;
if (element.scrollWidth <= defaultWidth &&
element.scrollHeight <= defaultHeight) {
bestSize = testSize;
} else {
break;
}
}
element.style.fontSize = `${bestSize}px`;
};

// Increase padding at end to "squeeze" text, until just before
// it gets squeezed so much that it gets longer vertically.
let avoidWidows = function (element) {
element.style.paddingRight = "0px";
let originalWidth = element.scrollWidth;
let originalHeight = element.scrollHeight;
let bestPadding;
for (let testPadding = 0; testPadding < originalWidth; testPadding += 0.5) {
element.style.paddingRight = `${testPadding}px`;
if (element.scrollHeight <= originalHeight) {
bestPadding = testPadding;
} else {
break;
}
}
element.style.paddingRight = `${bestPadding}px`;
if (window.getComputedStyle(element).direction === "rtl") {
element.style.paddingLeft = element.style.paddingRight;
element.style.paddingRight = "0px";
}
};

// Resize the text inside banner to fit.
let updateTextSizes = function () {
fitTextInElement(sel("#banner-tagline"));
fitTextInElement(sel("#banner-slogan"));
fitTextInElement(sel("#banner-donate-button-inner"));
avoidWidows(sel("#banner-tagline span"));
};

// Returns a random integer x, such that 0 <= x < max
let randomInteger = max => Math.floor(max * Math.random());

// The main donation banner function.
let runDonationBanner = function ({ taglines, slogan, donate, shortLocale }) {
try {
sel("#banner-tagline span").innerText = taglines[randomInteger(taglines.length)];
sel("#banner-slogan span").innerText = slogan;
let donateButtonText = sel("#banner-donate-button-inner span");
let rtl = window.getComputedStyle(donateButtonText).direction === "rtl";
donateButtonText.innerHTML = donate + "&#160;" + (rtl ? "&#9664;" : "&#9654;");
sel("#banner").style.display = "flex";
sel("#banner-spacer").style.display = "block";
addEventListener("resize", updateTextSizes);
updateTextSizes();
// Add a suffix corresponding to locale so we can send user
// to a correctly-localized donation page via redirect.
sel("#banner-donate-button-link").href += "-" + shortLocale;
sel("#torstatus-image").style.display = "none";
} catch (e) {
// Something went wrong.
console.error(e);
sel("#banner").style.display = "none";
sel("#bannerSpacer").style.display = "none";
sel("#torstatus-image").style.display = "block";
}
};

// Calls callback(attributeValue) when the specified attribute changes on
// target. Returns a zero-arg function that stops observing.
let observeAttribute = function (target, attributeName, callback) {
let observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === "attributes" &&
mutation.attributeName === attributeName) {
callback(target.getAttribute(attributeName));
}
});
});
observer.observe(target, { attributes: true });
return () => observer.disconnect();
};

// Start the donation banner if "toron" has been set to "yes".
let stopObserving = observeAttribute(document.body, "toron", value => {
stopObserving();
if (value === "yes") {
let bannerDataJSON = document.body.getAttribute("banner-data");
if (bannerDataJSON && bannerDataJSON.length > 0) {
runDonationBanner(JSON.parse(bannerDataJSON));
}
}
});
Binary file added src/chrome/content/aboutTor/onion-hand.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/chrome/content/torbutton.js
Expand Up @@ -13,6 +13,7 @@ let { showDialog } = Cu.import("resource://torbutton/modules/utils.js", {});
let { unescapeTorString } = Cu.import("resource://torbutton/modules/utils.js", {});
let SecurityPrefs = Cu.import("resource://torbutton/modules/security-prefs.js", {});
let { bindPrefAndInit, observe } = Cu.import("resource://torbutton/modules/utils.js", {});
let { bannerData } = Cu.import("resource://torbutton/modules/donation-banner.js", {});

const k_tb_last_browser_version_pref = "extensions.torbutton.lastBrowserVersion";
const k_tb_browser_update_needed_pref = "extensions.torbutton.updateNeeded";
Expand Down Expand Up @@ -450,7 +451,8 @@ var torbutton_abouttor_message_handler = {
torOn: torbutton_tor_check_ok(),
updateNeeded: torbutton_update_is_needed(),
showManual: torbutton_show_torbrowser_manual(),
toolbarButtonXPos: torbutton_get_toolbarbutton_xpos()
toolbarButtonXPos: torbutton_get_toolbarbutton_xpos(),
bannerData: bannerData(),
};
},

Expand Down
2 changes: 1 addition & 1 deletion src/chrome/locale/en/aboutTor.properties
Expand Up @@ -10,7 +10,7 @@ aboutTor.searchDDG.search.link=https://duckduckgo.com/

aboutTor.donationBanner.donate=Donate Now!

aboutTor.donationBanner.slogan=Tor: Powering Digital Resistance.
aboutTor.donationBanner.slogan=Tor: Powering Digital Resistance
aboutTor.donationBanner.mozilla=Give today and Mozilla will match your gift!

aboutTor.donationBanner.tagline1=Protecting Journalists, Whistleblowers, & Activists Since 2006
Expand Down
113 changes: 113 additions & 0 deletions src/chrome/skin/donation_banner.css
@@ -0,0 +1,113 @@
#banner {
-khtml-user-select: none; /* Konqueror */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Chrome/Safari/Opera */
display: none;
height: 150px;
justify-content: center;
left: 0px;
margin-top: 0px;
min-width: 900px;
opacity: 1;
position: absolute;
user-select: none;
width: 100%;
z-index: 1;
}
#banner:before {
background-color: #406;
background-image: url('resource://torbutton/chrome/content/aboutTor/onion-hand.png');
background-position: center;
background-size: cover;
content: "";
height: 150px;
left: 0px;
position: absolute;
top: 0px;
width: 100%;
}
#banner:-moz-dir(rtl):before {
transform: scaleX(-1);
}
#banner-contents-container {
align-items: center;
height: 100%;
max-width: 700px;
position: relative;
width: 700px;
}
#banner-tagline {
align-items: center;
bottom: 60px;
color: white;
display: flex;
font-family: monospace;
font-size: 8px;
font-weight: bold;
left: 85px;
position: absolute;
right: 0px;
text-align: start;
text-transform: uppercase;
top: 10px;
}
#banner-tagline:-moz-dir(rtl) {
left: 0px;
right: 85px;
}
#banner-slogan {
align-items: start;
bottom: 0px;
color: #f8f8a0;
display: flex;
font-family: monospace;
font-weight: bold;
left: 85px;
position: absolute;
right: 285px;
text-align: start;
top: 100px;
white-space: nowrap;
}
#banner-slogan:-moz-dir(rtl) {
left: 285px;
right: 85px;
}
#banner-donate-button {
background-color: #13a513;
border: 0px;
bottom: 10px;
color: #fbf7ef;
font-family: sans-serif;
font-size: 12px;
font-weight: bold;
left: 430px;
letter-spacing: -0.00em;
position: absolute;
right: 0px;
top: 100px;
}
#banner-donate-button:-moz-dir(rtl) {
left: 0px;
right: 430px;
}
#banner-donate-button:hover {
background-color: #38bc38;
}
#banner-donate-button-inner {
bottom: 6px;
display: flex;
justify-content: center;
left: 8px;
position: absolute;
right: 8px;
top: 6px;
}
#banner-spacer {
display: none;
height: 150px;
position: relative;
top: 0;
}

0 comments on commit b3ff986

Please sign in to comment.