| 1 |
/* |
| 2 |
* This file is part of Privacy Badger <https://privacybadger.org/> |
| 3 |
* Copyright (C) 2014 Electronic Frontier Foundation |
| 4 |
* |
| 5 |
* Derived from ShareMeNot |
| 6 |
* Copyright (C) 2011-2014 University of Washington |
| 7 |
* |
| 8 |
* Privacy Badger is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License version 3 as |
| 10 |
* published by the Free Software Foundation. |
| 11 |
* |
| 12 |
* Privacy Badger is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
* ShareMeNot is licensed under the MIT license: |
| 23 |
* http://www.opensource.org/licenses/mit-license.php |
| 24 |
* |
| 25 |
* Copyright (c) 2011-2014 University of Washington |
| 26 |
* |
| 27 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 28 |
* copy of this software and associated documentation files (the |
| 29 |
* "Software"), to deal in the Software without restriction, including |
| 30 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 31 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 32 |
* permit persons to whom the Software is furnished to do so, subject to |
| 33 |
* the following conditions: |
| 34 |
* |
| 35 |
* The above copyright notice and this permission notice shall be included |
| 36 |
* in all copies or substantial portions of the Software. |
| 37 |
* |
| 38 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 39 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 40 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 41 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 42 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 43 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 44 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 45 |
*/ |
| 46 |
|
| 47 |
(function () { |
| 48 |
|
| 49 |
// don't inject into non-HTML documents (such as XML documents) |
| 50 |
// but do inject into XHTML documents |
| 51 |
if (document instanceof HTMLDocument === false && ( |
| 52 |
document instanceof XMLDocument === false || |
| 53 |
document.createElement('div') instanceof HTMLDivElement === false |
| 54 |
)) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
function hasOwn(obj, prop) { |
| 59 |
return Object.prototype.hasOwnProperty.call(obj, prop); |
| 60 |
} |
| 61 |
|
| 62 |
// widget data |
| 63 |
let widgetList; |
| 64 |
|
| 65 |
// cached chrome.i18n.getMessage() results |
| 66 |
const TRANSLATIONS = {}; |
| 67 |
|
| 68 |
// references to widget page elements |
| 69 |
const WIDGET_ELS = {}; |
| 70 |
|
| 71 |
let doNotReplace = new WeakSet(); |
| 72 |
|
| 73 |
// if the widget element lacks a src property, |
| 74 |
// try to use the following dataset properties instead |
| 75 |
const lazyLoadDatasetSrcProps = [ |
| 76 |
"src", |
| 77 |
"ezsrc" |
| 78 |
]; |
| 79 |
|
| 80 |
/** |
| 81 |
* @param {Object} response response to checkWidgetReplacementEnabled |
| 82 |
*/ |
| 83 |
function init(response) { |
| 84 |
const FRAME_ID = response.frameId; |
| 85 |
|
| 86 |
for (const key in response.translations) { |
| 87 |
TRANSLATIONS[key] = response.translations[key]; |
| 88 |
} |
| 89 |
|
| 90 |
widgetList = response.widgetList; |
| 91 |
|
| 92 |
// check for widgets blocked before we got here |
| 93 |
replaceInitialTrackerButtonsHelper(response.widgetsToReplace); |
| 94 |
|
| 95 |
// set up listener for dynamically created widgets |
| 96 |
chrome.runtime.onMessage.addListener(function (request) { |
| 97 |
// blocked something, see if this is a widget domain that should be replaced |
| 98 |
if (request.type == "replaceWidget") { |
| 99 |
if (request.frameId === FRAME_ID) { |
| 100 |
replaceSubsequentTrackerButtonsHelper(request.trackerDomain); |
| 101 |
} |
| 102 |
|
| 103 |
// widget replacement initiated by a surrogate script |
| 104 |
} else if (request.type == "replaceWidgetFromSurrogate") { |
| 105 |
if (request.frameId === FRAME_ID) { |
| 106 |
replaceIndividualButton(request.widget); |
| 107 |
} |
| 108 |
} |
| 109 |
}); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Creates a replacement placeholder element for the given widget. |
| 114 |
* |
| 115 |
* @param {Object} widget the SocialWidget object |
| 116 |
* @param {Element} trackerElem the button/widget element we are replacing |
| 117 |
* @param {Function} callback called with the replacement element |
| 118 |
*/ |
| 119 |
function createReplacementElement(widget, trackerElem, callback) { |
| 120 |
let buttonData = widget.replacementButton; |
| 121 |
|
| 122 |
// no image data to fetch |
| 123 |
if (!hasOwn(buttonData, 'imagePath')) { |
| 124 |
return setTimeout(function () { |
| 125 |
_createReplacementElementCallback(widget, trackerElem, callback); |
| 126 |
}, 0); |
| 127 |
} |
| 128 |
|
| 129 |
// already have replacement button image URI cached |
| 130 |
if (buttonData.buttonUrl) { |
| 131 |
return setTimeout(function () { |
| 132 |
_createReplacementElementCallback(widget, trackerElem, callback); |
| 133 |
}, 0); |
| 134 |
} |
| 135 |
|
| 136 |
// already messaged for but haven't yet received the image data |
| 137 |
if (buttonData.loading) { |
| 138 |
// check back in 10 ms |
| 139 |
return setTimeout(function () { |
| 140 |
createReplacementElement(widget, trackerElem, callback); |
| 141 |
}, 10); |
| 142 |
} |
| 143 |
|
| 144 |
// don't have image data cached yet, get it from the background page |
| 145 |
buttonData.loading = true; |
| 146 |
chrome.runtime.sendMessage({ |
| 147 |
type: "getReplacementButton", |
| 148 |
widgetName: widget.name |
| 149 |
}, function (response) { |
| 150 |
if (response) { |
| 151 |
buttonData.buttonUrl = response; // cache image data |
| 152 |
_createReplacementElementCallback(widget, trackerElem, callback); |
| 153 |
} |
| 154 |
}); |
| 155 |
} |
| 156 |
|
| 157 |
function _createReplacementElementCallback(widget, trackerElem, callback) { |
| 158 |
if (widget.replacementButton.buttonUrl) { |
| 159 |
_createButtonReplacement(widget, callback); |
| 160 |
} else { |
| 161 |
_createWidgetReplacement(widget, trackerElem, callback); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
function _createButtonReplacement(widget, callback) { |
| 166 |
let buttonData = widget.replacementButton, |
| 167 |
button_type = buttonData.type; |
| 168 |
|
| 169 |
let button = document.createElement("img"); |
| 170 |
button.setAttribute("src", buttonData.buttonUrl); |
| 171 |
|
| 172 |
// TODO use custom tooltip to support RTL locales? |
| 173 |
button.setAttribute( |
| 174 |
"title", |
| 175 |
TRANSLATIONS.social_tooltip_pb_has_replaced.replace("XXX", widget.name) |
| 176 |
); |
| 177 |
|
| 178 |
let styleAttrs = [ |
| 179 |
"border: none", |
| 180 |
"cursor: pointer", |
| 181 |
"height: auto", |
| 182 |
"width: auto", |
| 183 |
]; |
| 184 |
button.setAttribute("style", styleAttrs.join(" !important;") + " !important"); |
| 185 |
|
| 186 |
// normal button type; just open a new window when clicked |
| 187 |
if (button_type === 0) { |
| 188 |
let popup_url = buttonData.details + encodeURIComponent(window.location.href); |
| 189 |
|
| 190 |
button.addEventListener("click", function (e) { |
| 191 |
if (!e.isTrusted) { return; } |
| 192 |
window.open(popup_url); |
| 193 |
}); |
| 194 |
|
| 195 |
// in place button type; replace the existing button |
| 196 |
// with an iframe when clicked |
| 197 |
} else if (button_type == 1) { |
| 198 |
let iframe_url = buttonData.details + encodeURIComponent(window.location.href); |
| 199 |
|
| 200 |
button.addEventListener("click", function (e) { |
| 201 |
if (!e.isTrusted) { return; } |
| 202 |
replaceButtonWithIframeAndUnblockTracker(button, widget.name, iframe_url); |
| 203 |
}, { once: true }); |
| 204 |
} |
| 205 |
|
| 206 |
callback(button); |
| 207 |
} |
| 208 |
|
| 209 |
function _createWidgetReplacement(widget, trackerElem, callback) { |
| 210 |
let replacementEl; |
| 211 |
|
| 212 |
// in-place widget types: |
| 213 |
// |
| 214 |
// type 3: |
| 215 |
// reinitialize the widget by reinserting its element's HTML |
| 216 |
// |
| 217 |
// type 4: |
| 218 |
// reinitialize the widget by reinserting its element's HTML |
| 219 |
// and activating associated scripts |
| 220 |
if ([3, 4].includes(widget.replacementButton.type)) { |
| 221 |
replacementEl = createReplacementWidget(widget, trackerElem); |
| 222 |
} |
| 223 |
|
| 224 |
callback(replacementEl); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Unblocks the given widget and replaces the given button with an iframe |
| 229 |
* pointing to the given URL. |
| 230 |
* |
| 231 |
* @param {Element} button the DOM element of the button to replace |
| 232 |
* @param {String} widget_name the name of the replacement widget |
| 233 |
* @param {String} iframeUrl the URL of the iframe to replace the button |
| 234 |
*/ |
| 235 |
function replaceButtonWithIframeAndUnblockTracker(button, widget_name, iframeUrl) { |
| 236 |
unblockTracker(widget_name, function () { |
| 237 |
// check is needed as for an unknown reason this callback function is |
| 238 |
// executed for buttons that have already been removed; we are trying |
| 239 |
// to prevent replacing an already removed button |
| 240 |
if (button.parentNode !== null) { |
| 241 |
let iframe = document.createElement("iframe"); |
| 242 |
|
| 243 |
iframe.setAttribute("src", iframeUrl); |
| 244 |
iframe.setAttribute("style", "border: none !important; height: 1.5em !important;"); |
| 245 |
|
| 246 |
button.parentNode.replaceChild(iframe, button); |
| 247 |
} |
| 248 |
}); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Unblocks the given widget and replaces our replacement placeholder |
| 253 |
* with the original third-party widget element. |
| 254 |
* |
| 255 |
* Reruns scripts defined in scriptSelectors, if any. |
| 256 |
* |
| 257 |
* The teardown to the initialization defined in createReplacementWidget(). |
| 258 |
*/ |
| 259 |
function restoreWidget(widget) { |
| 260 |
let name = widget.name; |
| 261 |
|
| 262 |
if (widget.reloadOnActivation) { |
| 263 |
unblockTracker(name, function () { |
| 264 |
location.reload(); |
| 265 |
}); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
if (widget.scriptSelectors) { |
| 270 |
if (widget.scriptSelectors.some(i => i.includes("onload\\=vueRecaptchaApiLoaded"))) { |
| 271 |
// we can't do "in-place" activation; reload the page instead |
| 272 |
unblockTracker(name, function () { |
| 273 |
location.reload(); |
| 274 |
}); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
// if there are no matching script elements |
| 279 |
if (!document.querySelectorAll(widget.scriptSelectors.join(',')).length) { |
| 280 |
// we can't do "in-place" activation; reload the page instead |
| 281 |
unblockTracker(name, function () { |
| 282 |
location.reload(); |
| 283 |
}); |
| 284 |
return; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
unblockTracker(name, function () { |
| 289 |
// restore all widgets of this type |
| 290 |
WIDGET_ELS[name].forEach(data => { |
| 291 |
if (!data.origWidgetElem.src) { |
| 292 |
for (let prop of lazyLoadDatasetSrcProps) { |
| 293 |
if (data.origWidgetElem.dataset[prop]) { |
| 294 |
data.origWidgetElem.src = data.origWidgetElem.dataset[prop]; |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
data.parentNode.replaceChild(data.origWidgetElem, data.replacement); |
| 300 |
if (data.scriptSelectors) { |
| 301 |
// This is part of "click-to-play" for third-party page widgets: |
| 302 |
// https://privacybadger.org/#How-does-Privacy-Badger-handle-social-media-widgets |
| 303 |
// |
| 304 |
// This is the part where the user chooses to activate the widget. |
| 305 |
// Some widgets are driven by JavaScript; their JavaScript needs |
| 306 |
// to be reloaded in order for the widget to function. |
| 307 |
// |
| 308 |
// Privacy Badger empowers the user to load certain widgets on demand, |
| 309 |
// instead of continuing to let them load by default, without a choice. |
| 310 |
// |
| 311 |
// Any script reinserted here is a script that would have |
| 312 |
// run on the page anyway, had Privacy Badger not blocked it. |
| 313 |
// This should not fall under remote code review considerations. |
| 314 |
reloadScripts(data.scriptSelectors); |
| 315 |
} |
| 316 |
}); |
| 317 |
WIDGET_ELS[name] = []; |
| 318 |
}); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Find and replace script elements with their copies to trigger re-running. |
| 323 |
* |
| 324 |
* This is code for re-activating a previously blocked third-party widget |
| 325 |
* (such as Google reCAPTCHA or Disqus comments). |
| 326 |
* |
| 327 |
* The scripts being run are third-party widget scripts that Privacy Badger |
| 328 |
* previously blocked and the user chose to activate. |
| 329 |
* |
| 330 |
* For example: |
| 331 |
* |
| 332 |
* 1. The user visits a page with comments powered by Disqus. |
| 333 |
* 2. Privacy Badger blocks the Disqus script and inserts a placeholder |
| 334 |
* where the Disqus widget would have appeared. |
| 335 |
* 3. If the user chooses to click "Allow" in the placeholder, Privacy Badger |
| 336 |
* removes the placeholder and reinserts the Disqus script. |
| 337 |
* |
| 338 |
* Any script reinserted here is a script that would have |
| 339 |
* run on the page anyway, had Privacy Badger not blocked it. |
| 340 |
*/ |
| 341 |
function reloadScripts(selectors) { |
| 342 |
let scripts = document.querySelectorAll(selectors.join(',')); |
| 343 |
|
| 344 |
for (let scriptEl of scripts) { |
| 345 |
// reinsert script elements only |
| 346 |
if (!scriptEl.nodeName || scriptEl.nodeName.toLowerCase() != 'script') { |
| 347 |
continue; |
| 348 |
} |
| 349 |
|
| 350 |
let replacement = document.createElement("script"); |
| 351 |
for (let attr of scriptEl.attributes) { |
| 352 |
replacement.setAttribute(attr.nodeName, attr.value); |
| 353 |
} |
| 354 |
scriptEl.parentNode.replaceChild(replacement, scriptEl); |
| 355 |
// reinsert one script and quit |
| 356 |
break; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Replaces all tracker buttons on the current web page with the internal |
| 362 |
* replacement buttons, respecting the user's blocking settings. |
| 363 |
* |
| 364 |
* @param {Object} widgetsToReplace an object with keys set to widget names |
| 365 |
*/ |
| 366 |
function replaceInitialTrackerButtonsHelper(widgetsToReplace) { |
| 367 |
widgetList.forEach(function (widget) { |
| 368 |
if (hasOwn(widgetsToReplace, widget.name)) { |
| 369 |
replaceIndividualButton(widget); |
| 370 |
} |
| 371 |
}); |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Individually replaces tracker buttons blocked after initial check. |
| 376 |
*/ |
| 377 |
function replaceSubsequentTrackerButtonsHelper(tracker_domain) { |
| 378 |
if (!widgetList) { |
| 379 |
return; |
| 380 |
} |
| 381 |
widgetList.forEach(function (widget) { |
| 382 |
let replace = widget.domains.some(domain => { |
| 383 |
if (domain == tracker_domain) { |
| 384 |
return true; |
| 385 |
// leading wildcard |
| 386 |
} else if (domain[0] == "*") { |
| 387 |
if (tracker_domain.endsWith(domain.slice(1))) { |
| 388 |
return true; |
| 389 |
} |
| 390 |
} |
| 391 |
return false; |
| 392 |
}); |
| 393 |
if (replace) { |
| 394 |
replaceIndividualButton(widget); |
| 395 |
} |
| 396 |
}); |
| 397 |
} |
| 398 |
|
| 399 |
function _make_id(prefix) { |
| 400 |
return prefix + "-" + Math.random().toString().replace(".", ""); |
| 401 |
} |
| 402 |
|
| 403 |
function createReplacementWidget(widget, elToReplace) { |
| 404 |
if (!elToReplace.parentNode) { |
| 405 |
return null; |
| 406 |
} |
| 407 |
|
| 408 |
let name = widget.name; |
| 409 |
|
| 410 |
let widgetFrame = document.createElement('iframe'); |
| 411 |
|
| 412 |
// widget replacement frame styles |
| 413 |
let border_width = 1; |
| 414 |
let styleAttrs = [ |
| 415 |
"background-color: #fff", |
| 416 |
"border: " + border_width + "px solid #ec9329", |
| 417 |
"min-width: 220px", |
| 418 |
"min-height: 210px", |
| 419 |
"max-height: 600px", |
| 420 |
"pointer-events: all", |
| 421 |
"z-index: 999", |
| 422 |
]; |
| 423 |
// TODO shouldn't need this (nor !important, nor _make_id, nor ...) if we use shadow DOM |
| 424 |
let elToReplaceStyles = window.getComputedStyle(elToReplace); |
| 425 |
if (elToReplaceStyles.position == "absolute") { |
| 426 |
styleAttrs.push("position: absolute"); |
| 427 |
for (let prop of ["width", "height", "top", "right", "bottom", "left"]) { |
| 428 |
let val = elToReplaceStyles[prop]; |
| 429 |
if (!val) { |
| 430 |
continue; |
| 431 |
} |
| 432 |
if (prop == "width" || prop == "height") { |
| 433 |
if (elToReplaceStyles['box-sizing'] == 'content-box') { |
| 434 |
if (Number.isInteger(val) || val.endsWith("px")) { |
| 435 |
val = `${parseInt(val, 10) - 2*border_width}px`; |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
styleAttrs.push(prop + ": " + val); |
| 440 |
} |
| 441 |
} else { |
| 442 |
if (elToReplace.offsetWidth > 0) { |
| 443 |
if (elToReplaceStyles['box-sizing'] == 'content-box') { |
| 444 |
styleAttrs.push(`width: ${elToReplace.offsetWidth - 2*border_width}px`); |
| 445 |
} else { |
| 446 |
styleAttrs.push(`width: ${elToReplace.offsetWidth}px`); |
| 447 |
} |
| 448 |
} |
| 449 |
if (elToReplace.offsetHeight > 0) { |
| 450 |
if (elToReplaceStyles['box-sizing'] == 'content-box') { |
| 451 |
styleAttrs.push(`height: ${elToReplace.offsetHeight - 2*border_width}px`); |
| 452 |
} else { |
| 453 |
styleAttrs.push(`height: ${elToReplace.offsetHeight}px`); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
widgetFrame.style = styleAttrs.join(" !important;") + " !important"; |
| 458 |
|
| 459 |
let widgetDiv = document.createElement('div'); |
| 460 |
|
| 461 |
// parent div styles |
| 462 |
styleAttrs = [ |
| 463 |
"display: flex", |
| 464 |
"flex-direction: column", |
| 465 |
"align-items: center", |
| 466 |
"justify-content: center", |
| 467 |
"width: 100%", |
| 468 |
"height: 100%", |
| 469 |
]; |
| 470 |
if (TRANSLATIONS.rtl) { |
| 471 |
styleAttrs.push("direction: rtl"); |
| 472 |
} |
| 473 |
widgetDiv.style = styleAttrs.join(" !important;") + " !important"; |
| 474 |
|
| 475 |
// child div styles |
| 476 |
styleAttrs = [ |
| 477 |
"font-family: helvetica, arial, sans-serif", |
| 478 |
"font-size: 16px", |
| 479 |
"display: flex", |
| 480 |
"flex-wrap: wrap", |
| 481 |
"justify-content: center", |
| 482 |
"text-align: center", |
| 483 |
"margin: 10px", |
| 484 |
]; |
| 485 |
|
| 486 |
let textDiv = document.createElement('div'); |
| 487 |
textDiv.style = styleAttrs.join(" !important;") + " !important"; |
| 488 |
|
| 489 |
let summary = TRANSLATIONS.widget_placeholder_pb_has_replaced.replace("XXX", name), |
| 490 |
link_start = "YYY", |
| 491 |
link_end = "ZZZ"; |
| 492 |
|
| 493 |
// get a direct link to widget content when available |
| 494 |
let widget_url, |
| 495 |
node_name = elToReplace.nodeName.toLowerCase(); |
| 496 |
if (widget.directLinkUrl) { |
| 497 |
widget_url = widget.directLinkUrl; |
| 498 |
} else if (node_name == 'iframe' && !widget.noDirectLink) { |
| 499 |
// use the frame URL for framed widgets |
| 500 |
if (elToReplace.src) { |
| 501 |
widget_url = elToReplace.src; |
| 502 |
if (widget.name == "YouTube") { |
| 503 |
let yuri = (new URL(widget_url)), |
| 504 |
ypath = yuri.pathname.slice(1).split("/"); |
| 505 |
if (ypath.length == 2 && ypath[0] == "embed" && |
| 506 |
ypath[1].length == 11) { |
| 507 |
if (ypath[1] == "videoseries" || ypath[1] == "live_stream") { |
| 508 |
widget_url = null; |
| 509 |
} else { |
| 510 |
let ysearch = (yuri.search ? "&" + yuri.search.slice(1) : ""); |
| 511 |
widget_url = "https://www.youtube.com/watch?v=" + ypath[1] + ysearch; |
| 512 |
} |
| 513 |
} |
| 514 |
} else if (widget_url.startsWith("https://embed.bsky.app/embed/")) { |
| 515 |
// Bluesky |
| 516 |
let buri = (new URL(widget_url)).pathname.split("/"); |
| 517 |
if (buri[2] && buri[2].startsWith("did:") && buri[4]) { |
| 518 |
widget_url = "https://bsky.app/profile/" + buri[2] + "/post/" + buri[4]; |
| 519 |
} |
| 520 |
} |
| 521 |
} else { |
| 522 |
for (let prop of lazyLoadDatasetSrcProps) { |
| 523 |
if (elToReplace.dataset[prop]) { |
| 524 |
widget_url = elToReplace.dataset[prop]; |
| 525 |
break; |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} else if (node_name == 'blockquote') { |
| 530 |
if (elToReplace.cite && elToReplace.cite.startsWith('https://www.tiktok.com/@')) { |
| 531 |
// TikTok |
| 532 |
widget_url = elToReplace.cite; |
| 533 |
} else if (elToReplace.className.includes("twitter-tweet") || elToReplace.className.includes("twitter-video")) { |
| 534 |
// Twitter |
| 535 |
let lastLink = Array.from(elToReplace.querySelectorAll("a[href^='https://twitter.com/'], a[href^='https://x.com/']")).slice(-1)[0]; |
| 536 |
if (lastLink) { |
| 537 |
widget_url = lastLink.href; |
| 538 |
} |
| 539 |
} else if (elToReplace.dataset && elToReplace.dataset.textPostPermalink && elToReplace.dataset.textPostPermalink.startsWith('https://www.threads.net/')) { |
| 540 |
// Threads |
| 541 |
widget_url = elToReplace.dataset.textPostPermalink; |
| 542 |
} else if (elToReplace.dataset && elToReplace.dataset.blueskyUri) { |
| 543 |
// Bluesky |
| 544 |
let buri = elToReplace.dataset.blueskyUri.split('/'); |
| 545 |
if (buri[0] && buri[0] == "at:" && buri[2] && buri[2].startsWith("did:") && buri[4]) { |
| 546 |
widget_url = "https://bsky.app/profile/" + buri[2] + "/post/" + buri[4]; |
| 547 |
} |
| 548 |
} else if (elToReplace.dataset && elToReplace.dataset.instgrmPermalink && elToReplace.dataset.instgrmPermalink.startsWith('https://www.instagram.com/')) { |
| 549 |
// Instagram |
| 550 |
widget_url = elToReplace.dataset.instgrmPermalink; |
| 551 |
} |
| 552 |
} else if (node_name == 'amp-twitter') { |
| 553 |
// AMP Twitter |
| 554 |
let tweet_id = elToReplace.dataset && elToReplace.dataset.tweetid && |
| 555 |
elToReplace.dataset.tweetid.replace(/[^0-9]/g, ''); |
| 556 |
if (tweet_id) { |
| 557 |
widget_url = "https://twitter.com/x/status/" + tweet_id; |
| 558 |
} |
| 559 |
} else if (node_name == 'amp-instagram') { |
| 560 |
// AMP Instagram |
| 561 |
let shortcode = elToReplace.dataset && elToReplace.dataset.shortcode && |
| 562 |
elToReplace.dataset.shortcode.replace(/^0-9A-Za-z/g, ''); |
| 563 |
if (shortcode) { |
| 564 |
widget_url = "https://www.instagram.com/p/" + shortcode; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
if (widget_url) { |
| 569 |
// construct link to original widget frame |
| 570 |
let text_before = summary.slice(0, summary.indexOf(link_start)), |
| 571 |
text_after = summary.slice(summary.indexOf(link_end) + link_end.length), |
| 572 |
link_text = summary.slice( |
| 573 |
summary.indexOf(link_start) + link_start.length, summary.indexOf(link_end)); |
| 574 |
|
| 575 |
// nest in a wrapper to preserve whitespace (flexbox) |
| 576 |
let wrapperDiv = document.createElement("div"); |
| 577 |
|
| 578 |
if (text_before) { |
| 579 |
wrapperDiv.appendChild(document.createTextNode(text_before)); |
| 580 |
} |
| 581 |
|
| 582 |
let widgetLink = document.createElement("a"); |
| 583 |
widgetLink.href = widget_url; |
| 584 |
widgetLink.rel = "noreferrer"; |
| 585 |
widgetLink.target = "_blank"; |
| 586 |
widgetLink.appendChild(document.createTextNode(link_text)); |
| 587 |
wrapperDiv.appendChild(widgetLink); |
| 588 |
|
| 589 |
if (text_after) { |
| 590 |
wrapperDiv.appendChild(document.createTextNode(text_after)); |
| 591 |
} |
| 592 |
|
| 593 |
textDiv.appendChild(wrapperDiv); |
| 594 |
|
| 595 |
} else { |
| 596 |
// no link to construct, remove the link markers |
| 597 |
summary = summary.replace(link_start, "").replace(link_end, ""); |
| 598 |
textDiv.appendChild(document.createTextNode(summary)); |
| 599 |
} |
| 600 |
|
| 601 |
let closeIcon = document.createElement('a'), |
| 602 |
close_icon_id = _make_id("ico-close"); |
| 603 |
closeIcon.id = close_icon_id; |
| 604 |
closeIcon.href = "javascript:void(0)"; // eslint-disable-line no-script-url |
| 605 |
textDiv.appendChild(closeIcon); |
| 606 |
|
| 607 |
let infoIcon = document.createElement('a'), |
| 608 |
info_icon_id = _make_id("ico-help"); |
| 609 |
infoIcon.id = info_icon_id; |
| 610 |
infoIcon.href = "https://privacybadger.org/#How-does-Privacy-Badger-handle-social-media-widgets"; |
| 611 |
infoIcon.rel = "noreferrer"; |
| 612 |
infoIcon.target = "_blank"; |
| 613 |
textDiv.appendChild(infoIcon); |
| 614 |
widgetDiv.appendChild(textDiv); |
| 615 |
|
| 616 |
let buttonDiv = document.createElement('div'); |
| 617 |
styleAttrs.push("width: 100%"); |
| 618 |
buttonDiv.style = styleAttrs.join(" !important;") + " !important"; |
| 619 |
|
| 620 |
// allow once button |
| 621 |
let button = document.createElement('button'), |
| 622 |
button_id = _make_id("btn-once"); |
| 623 |
button.id = button_id; |
| 624 |
styleAttrs = [ |
| 625 |
"transition: background-color 0.25s ease-out, border-color 0.25s ease-out, color 0.25s ease-out", |
| 626 |
"border-radius: 3px", |
| 627 |
"cursor: pointer", |
| 628 |
// systemfontstack.com |
| 629 |
"font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, liberation sans, Ubuntu, helvetica neue, helvetica, Cantarell, roboto, noto, arial, sans-serif", |
| 630 |
"font-size: 14px", |
| 631 |
"font-weight: bold", |
| 632 |
// fix overly bold text on macOS |
| 633 |
"-webkit-font-smoothing: antialiased", |
| 634 |
"-moz-osx-font-smoothing: grayscale", |
| 635 |
"line-height: 16px", |
| 636 |
"padding: 10px", |
| 637 |
"margin: 4px", |
| 638 |
"width: 70%", |
| 639 |
"max-width: 280px", |
| 640 |
]; |
| 641 |
button.style = styleAttrs.join(" !important;") + " !important"; |
| 642 |
|
| 643 |
// allow on this site button |
| 644 |
let site_button = document.createElement('button'), |
| 645 |
site_button_id = _make_id("btn-site"); |
| 646 |
site_button.id = site_button_id; |
| 647 |
site_button.style = styleAttrs.join(" !important;") + " !important"; |
| 648 |
|
| 649 |
button.appendChild(document.createTextNode(TRANSLATIONS.allow_once)); |
| 650 |
site_button.appendChild(document.createTextNode(TRANSLATIONS.allow_on_site)); |
| 651 |
|
| 652 |
buttonDiv.appendChild(button); |
| 653 |
buttonDiv.appendChild(site_button); |
| 654 |
|
| 655 |
widgetDiv.appendChild(buttonDiv); |
| 656 |
|
| 657 |
// save refs. to elements for use in teardown |
| 658 |
if (!hasOwn(WIDGET_ELS, name)) { |
| 659 |
WIDGET_ELS[name] = []; |
| 660 |
} |
| 661 |
let data = { |
| 662 |
parentNode: elToReplace.parentNode, |
| 663 |
replacement: widgetFrame, |
| 664 |
origWidgetElem: elToReplace |
| 665 |
}; |
| 666 |
if (widget.scriptSelectors) { |
| 667 |
data.scriptSelectors = widget.scriptSelectors; |
| 668 |
} |
| 669 |
WIDGET_ELS[name].push(data); |
| 670 |
|
| 671 |
// set up click handler |
| 672 |
widgetFrame.addEventListener('load', function () { |
| 673 |
let onceButton = widgetFrame.contentDocument.getElementById(button_id), |
| 674 |
siteButton = widgetFrame.contentDocument.getElementById(site_button_id), |
| 675 |
closeLink = widgetFrame.contentDocument.getElementById(close_icon_id); |
| 676 |
|
| 677 |
onceButton.addEventListener("click", function (e) { |
| 678 |
if (!e.isTrusted) { return; } |
| 679 |
e.preventDefault(); |
| 680 |
restoreWidget(widget); |
| 681 |
}, { once: true }); |
| 682 |
|
| 683 |
siteButton.addEventListener("click", function (e) { |
| 684 |
if (!e.isTrusted) { |
| 685 |
return; |
| 686 |
} |
| 687 |
|
| 688 |
e.preventDefault(); |
| 689 |
|
| 690 |
// first message the background page to record that |
| 691 |
// this widget should always be allowed on this site |
| 692 |
chrome.runtime.sendMessage({ |
| 693 |
type: "allowWidgetOnSite", |
| 694 |
widgetName: name |
| 695 |
}, function () { |
| 696 |
restoreWidget(widget); |
| 697 |
}); |
| 698 |
}, { once: true }); |
| 699 |
|
| 700 |
closeLink.addEventListener("click", function (e) { |
| 701 |
if (!e.isTrusted) { |
| 702 |
return; |
| 703 |
} |
| 704 |
e.preventDefault(); |
| 705 |
WIDGET_ELS[name] = WIDGET_ELS[name].filter(d => d.replacement != widgetFrame); |
| 706 |
doNotReplace.add(elToReplace); |
| 707 |
widgetFrame.replaceWith(elToReplace); |
| 708 |
}, { once: true }); |
| 709 |
|
| 710 |
}, false); // end of click handler |
| 711 |
|
| 712 |
let head_styles = ` |
| 713 |
html, body { |
| 714 |
color: #303030 !important; |
| 715 |
height: 100% !important; |
| 716 |
overflow: hidden !important; |
| 717 |
} |
| 718 |
#${button_id} { |
| 719 |
border: 2px solid #f06a0a !important; |
| 720 |
background-color: #f06a0a !important; |
| 721 |
color: #fefefe !important; |
| 722 |
} |
| 723 |
#${site_button_id} { |
| 724 |
border: 2px solid #333 !important; |
| 725 |
background-color: #fefefe !important; |
| 726 |
color: #333 !important; |
| 727 |
} |
| 728 |
#${button_id}:hover { |
| 729 |
background-color: #fefefe !important; |
| 730 |
color: #333 !important; |
| 731 |
} |
| 732 |
#${site_button_id}:hover { |
| 733 |
background-color: #fefefe !important; |
| 734 |
border: 2px solid #f06a0a !important; |
| 735 |
} |
| 736 |
#${info_icon_id}, #${close_icon_id} { |
| 737 |
position: absolute; |
| 738 |
${TRANSLATIONS.rtl ? "left" : "right"}: 4px; |
| 739 |
top: 4px; |
| 740 |
text-align: center; |
| 741 |
text-decoration: none; |
| 742 |
} |
| 743 |
#${close_icon_id} { |
| 744 |
${TRANSLATIONS.rtl ? "right" : "left"}: 4px; |
| 745 |
width: 20px; |
| 746 |
${TRANSLATIONS.rtl ? "left" : "right"}: unset; |
| 747 |
} |
| 748 |
#${info_icon_id}:before, #${close_icon_id}:before { |
| 749 |
border: 2px solid; |
| 750 |
border-radius: 50%; |
| 751 |
display: inline-flex; |
| 752 |
justify-content: center; |
| 753 |
align-items: center; |
| 754 |
color: #555; |
| 755 |
content: '?'; |
| 756 |
font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, liberation sans, Ubuntu, helvetica neue, helvetica, Cantarell, roboto, noto, arial, sans-serif; |
| 757 |
font-size: 12px; |
| 758 |
font-weight: bold; |
| 759 |
-webkit-font-smoothing: antialiased; |
| 760 |
-moz-osx-font-smoothing: grayscale; |
| 761 |
padding: 1px; |
| 762 |
height: 1em; |
| 763 |
width: 1em; |
| 764 |
} |
| 765 |
#${close_icon_id}:before { |
| 766 |
border: 0; |
| 767 |
content: '\u2715'; |
| 768 |
padding: 4px; |
| 769 |
} |
| 770 |
#${info_icon_id}:hover:before, #${close_icon_id}:hover:before { |
| 771 |
color: #ec9329; |
| 772 |
} |
| 773 |
a { |
| 774 |
text-decoration: underline; |
| 775 |
color: black; |
| 776 |
} |
| 777 |
a:hover { |
| 778 |
color: #ec9329; |
| 779 |
} |
| 780 |
@media (prefers-color-scheme: dark) { |
| 781 |
:root { |
| 782 |
color-scheme: dark; |
| 783 |
} |
| 784 |
body { |
| 785 |
background-color: #333 !important; |
| 786 |
color: #ddd !important; |
| 787 |
} |
| 788 |
a, a:visited { |
| 789 |
color: #ddd !important; |
| 790 |
} |
| 791 |
a:hover { |
| 792 |
color: #f06a0a !important; |
| 793 |
} |
| 794 |
#${info_icon_id}:before, #${close_icon_id}:before { |
| 795 |
color: #aaa; |
| 796 |
} |
| 797 |
#${site_button_id} { |
| 798 |
background-color: #333 !important; |
| 799 |
border: solid 2px #ddd !important; |
| 800 |
color: #ddd !important; |
| 801 |
} |
| 802 |
#${button_id}:hover, #${site_button_id}:hover { |
| 803 |
background-color: #333 !important; |
| 804 |
color: #ddd !important; |
| 805 |
} |
| 806 |
} |
| 807 |
`.trim(); |
| 808 |
|
| 809 |
widgetFrame.srcdoc = '<html><head><style>' + head_styles + '</style></head><body style="margin:0">' + widgetDiv.outerHTML + '</body></html>'; |
| 810 |
|
| 811 |
return widgetFrame; |
| 812 |
} |
| 813 |
|
| 814 |
/** |
| 815 |
* Replaces buttons/widgets in the DOM. |
| 816 |
*/ |
| 817 |
function replaceIndividualButton(widget) { |
| 818 |
let elsToReplace = []; |
| 819 |
|
| 820 |
if (widget.buttonSelectors) { |
| 821 |
elsToReplace = document.querySelectorAll(widget.buttonSelectors.join(',')); |
| 822 |
} else if (widget.selectors) { |
| 823 |
let selectors = []; |
| 824 |
for (let item of widget.selectors) { |
| 825 |
for (let url of item.urls) { |
| 826 |
selectors.push(`${item.elm}[src^='${url}']`); |
| 827 |
for (let prop of lazyLoadDatasetSrcProps) { |
| 828 |
selectors.push(`${item.elm}[data-${prop}^='${url}']`); |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
elsToReplace = document.querySelectorAll(selectors.join(',')); |
| 833 |
} |
| 834 |
|
| 835 |
for (let el of elsToReplace) { |
| 836 |
if (doNotReplace.has(el)) { |
| 837 |
continue; |
| 838 |
} |
| 839 |
// also don't replace if we think we currently have a placeholder |
| 840 |
// for this widget type attached to the same parent element |
| 841 |
if (hasOwn(WIDGET_ELS, widget.name)) { |
| 842 |
if (WIDGET_ELS[widget.name].some(d => d.parentNode == el.parentNode)) { |
| 843 |
// something went wrong, give up |
| 844 |
continue; |
| 845 |
} |
| 846 |
} |
| 847 |
// also don't replace if we're in an AMP frame, |
| 848 |
// as our placeholder sizing doesn't work inside AMP frames, |
| 849 |
// and we can instead replace higher-level <amp-*> elements |
| 850 |
if (document.location.hostname.endsWith(".ampproject.net")) { |
| 851 |
continue; |
| 852 |
} |
| 853 |
createReplacementElement(widget, el, function (replacementEl) { |
| 854 |
if (replacementEl) { |
| 855 |
el.parentNode.replaceChild(replacementEl, el); |
| 856 |
} |
| 857 |
}); |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
/** |
| 862 |
* Messages the background page to temporarily allow domains associated with a |
| 863 |
* given replacement widget. |
| 864 |
* Calls the provided callback function upon response. |
| 865 |
* |
| 866 |
* @param {String} name the name of the replacement widget |
| 867 |
* @param {Function} callback the callback function |
| 868 |
*/ |
| 869 |
function unblockTracker(name, callback) { |
| 870 |
let request = { |
| 871 |
type: "unblockWidget", |
| 872 |
widgetName: name |
| 873 |
}; |
| 874 |
chrome.runtime.sendMessage(request, callback); |
| 875 |
} |
| 876 |
|
| 877 |
// END FUNCTION DEFINITIONS /////////////////////////////////////////////////// |
| 878 |
|
| 879 |
chrome.runtime.sendMessage({ |
| 880 |
type: "checkWidgetReplacementEnabled" |
| 881 |
}, function (response) { |
| 882 |
if (!response) { |
| 883 |
return; |
| 884 |
} |
| 885 |
|
| 886 |
init(response); |
| 887 |
|
| 888 |
chrome.runtime.sendMessage({ |
| 889 |
type: "widgetReplacementReady" |
| 890 |
}); |
| 891 |
}); |
| 892 |
|
| 893 |
}()); |