summaryrefslogtreecommitdiff
path: root/data/extensions/https-everywhere@eff.org/pages/cancel/ux.js
blob: 259d6020f39b536abba90d9b440a7ac973e3d7d8 (plain)
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
/* global sendMessage */

"use strict";

let observer;
document.addEventListener("DOMContentLoaded", () => {
  const explainer = document.querySelector("[data-i18n=cancel_he_blocking_explainer]");
  observer = new MutationObserver(() => {
    replaceLink(explainer);
  });
  if (explainer.innerText.length > 0) {
    replaceLink(explainer);
  } else {
    observer.observe(explainer, {childList: true});
  }
  displayURL();
});

function replaceLink(explainer) {
  observer.disconnect();
  const linkText = chrome.i18n.getMessage("cancel_he_blocking_network");
  const link = document.createElement("a");
  link.classList.add("wikilink");
  link.href = "https://en.wikipedia.org/wiki/Downgrade_attack";
  link.innerText = linkText;
  explainer.innerHTML = explainer.innerHTML.replace(linkText, link.outerHTML);

  /*
    In response to translation of i18n string "cancel_he_blocking_network".
    Within context of the paragraph and as a standalone string can be interpreted differently
    langauge to language.

    So if link fails to swap in replace, this conditional is triggered
  */
  if (document.getElementsByClassName("wikilink").length === 0) {
    link.innerText = linkText;
    explainer.after(link);
  }

}

function displayURL() {
  const searchParams = new URLSearchParams(window.location.search);
  const originURL = searchParams.get('originURL');
  const originURLLink = document.getElementById('url-value');
  const openURLButton = document.getElementById('open-url-button');
  const openHttpOnce = document.getElementById('http-once-button');
  const copyButton = document.getElementById('copy-url');
  const url = new URL(originURL);

  originURLLink.innerText = originURL;
  originURLLink.href = originURL;

  openURLButton.addEventListener("click", function() {
    if (confirm(chrome.i18n.getMessage("cancel_open_page") + '?')) {
      sendMessage("disable_on_site", url.host, () => {
        window.location = originURL;
      });
    }

    return false;
  });

  // Copy URL Feature on EASE

  function copyLinkAlternate() {
    let isSuccessful = false;

    const sel = window.getSelection();

    try {
      sel.removeAllRanges();

      const range = document.createRange();
      range.selectNode(originURLLink);

      sel.addRange(range);

      isSuccessful = document.execCommand("copy");

      sel.removeAllRanges();

      return isSuccessful;
    } catch (err) {
      console.error(err);

      sel.removeAllRanges();

      return false;
    }
  }

  async function copyLink() {
    try {
      await navigator.clipboard.writeText(originURL);
      return true;
    } catch (err) {
      return copyLinkAlternate();
    }
  }

  let restoreTimeout = null;

  copyButton.addEventListener("click", async () => {
    if (await copyLink()) {
      copyButton.innerText = chrome.i18n.getMessage("cancel_copied_url");

      if (restoreTimeout !== null) {
        clearTimeout(restoreTimeout);
      }

      restoreTimeout = setTimeout(() => {
        copyButton.innerText = chrome.i18n.getMessage("cancel_copy_url");
        restoreTimeout = null;
      }, 1500);
    }
  });

  openHttpOnce.addEventListener("click", function() {
    if (confirm(chrome.i18n.getMessage("cancel_http_once") + '?')) {
      sendMessage("disable_on_site_once", url.host, () => {
        window.location = originURL;
      });
    }

    return false;
  });
}