aboutsummaryrefslogtreecommitdiff
path: root/html/display_panel/content/main_panel.js
blob: 7a5e2c3a3e4843d8d30a3a0740ab12d65185bdfb (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
* *
* Copyright (C) 2017, 2018 NateN1222 <nathannichols454@gmail.com>
* Copyright (C) 2018 Ruben Rodriguez <ruben@gnu.org>
* Copyright (C) 2018 Giorgio Maone <giorgio@maone.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program.  If not, see  <http://www.gnu.org/licenses/>.
*
*/

var fromTab = window.location.hash.match(/^#fromTab=(\d+)/) && RegExp.$1;
if (fromTab) {
  let browserStyle = document.createElement("link");
  browserStyle.rel = "stylesheet";
  browserStyle.href = "chrome://browser/content/extension.css";
  document.head.appendChild(browserStyle);
  document.documentElement.classList.add("tab");
}

var myPort = browser.runtime.connect({ name: "port-from-cs" });
var currentReport;

// Sends a message that tells the background script the window is open
myPort.postMessage({ "update": true, tabId: parseInt(currentReport && currentReport.tabId || fromTab) || "" });

// Display the actual extension version Number
document.querySelector("#version").textContent = browser.runtime.getManifest().version;

// Enable autotest button if this is a test-enabled build / session
(async () => {
  if (await Test.getURL()) {
    let button = document.querySelector("#autotest");
    button.style.display = "block";
    button.onclick = async () => {
      await Test.getTab(true);
      close();
    }
  }
})();

var liTemplate = document.querySelector("#li-template");
liTemplate.remove();

document.querySelector("#info").addEventListener("click", e => {
  let button = e.target;
  if (button.tagName === "A") {
    setTimeout(close, 100);
    return;
  }
  if (button.tagName !== "BUTTON") button = button.closest("button");
  if (button.matches(".toggle-source")) {
    let parent = button.parentNode;
    if (!parent.querySelector(".source").textContent) {
      parent.querySelector("a").click();
    } else {
      parent.classList.toggle("visible");
    }
    return;
  }
  if (!button.matches(".buttons > button")) return;
  let domain = button.querySelector(".domain");

  let li = button.closest("li");
  let entry = li && li._scriptEntry || [currentReport.url, "Page's site"];
  let action = button.className;
  let site = domain ? domain.textContent : button.name === "*" ? currentReport.site : "";

  if (site) {
    ([action] = action.split("-"));
  }
  myPort.postMessage({ [action]: entry, site, tabId: currentReport.tabId });
});

document.querySelector("#report-tab").onclick = e => {
  myPort.postMessage({ report_tab: currentReport });
  close();
}

document.querySelector("#complain").onclick = e => {
  myPort.postMessage({ invoke_contact_finder: currentReport });
  close();
}

document.querySelector("#open-options").onclick = e => {
  browser.runtime.openOptionsPage();
  close();
}

document.body.addEventListener("click", async e => {
  if (!e.target.matches(".reload")) return;
  let { tabId } = currentReport;
  if (tabId) {
    await browser.tabs.reload(tabId);
    myPort.postMessage({ "update": true, tabId });
  }
});

/*
*	Takes in the [[file_id, reason],...] array and the group name for one group
* of scripts found in this tab, rendering it as a list with management buttons.
*	Groups are "unknown", "blacklisted", "whitelisted", "accepted", and "blocked".
*/
function createList(data, group) {
  var { url } = data;
  let entries = data[group];
  let container = document.getElementById(group);
  let heading = container.querySelector("h2");
  var list = container.querySelector("ul");
  list.classList.toggle(group, true);
  if (Array.isArray(entries) && entries.length) {
    heading.innerHTML = `<span class="type-name">${group}</span> scripts in ${url}:`;
    container.classList.remove("empty");
  } else {
    // default message
    list.innerHTML = `<li>No <span class="type-name">${group}</span> scripts on this page.</li>`
    entries = data[group] = [];
    container.classList.add("empty");
  }
  // generate list
  let viewSourceToHuman = /^view-source:(.*)#line(\d+)\(([^)]*)\)[^]*/;
  for (let entry of entries) {
    let [scriptId, reason] = entry;
    let li = liTemplate.cloneNode(true);
    let a = li.querySelector("a");
    a.href = scriptId.split("(")[0];
    if (scriptId.startsWith("view-source:")) {
      a.target = "LibreJS-ViewSource";
      let source = scriptId.match(/\n([^]*)/);
      if (source) {
        li.querySelector(".source").textContent = source[1];
        li.querySelector(".toggle-source").style.display = "inline";
      }
      scriptId = scriptId.replace(viewSourceToHuman, "$3 at line $2 of $1");
    }
    a.textContent = scriptId;
    li.querySelector(".reason").textContent = reason;
    let bySite = !!reason.match(/https?:\/\/[^/]+\/\*/);
    li.classList.toggle("by-site", bySite);
    if (bySite) {
      let domain = li.querySelector(".forget .domain");
      if (domain) domain.textContent = RegExp.lastMatch;
    }
    li._scriptEntry = entry;
    list.appendChild(li);
  }

}

/**
* Updates scripts lists and buttons to act on them.
* If return_HTML is true, it returns the HTML of the popup window without updating it.
*	example report argument:
* {
*		"accepted": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]],
*		"blocked": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]],
*		"whitelisted": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]],
*		"blacklisted": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]],
*   "unknown": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]],
*		"url":"example.com"
*	};
*
*/
function refreshUI(report) {
  currentReport = report;
  let { siteStatus, listedSite } = report;
  document.querySelector("#site").className = siteStatus || "";
  document.querySelector("#site h2").textContent =
    `This site ${report.site}`;

  for (let toBeErased of document.querySelectorAll("#info h2:not(.site) > *, #info ul > *")) {
    toBeErased.remove();
  }

  let scriptsCount = 0;
  for (let group of ["unknown", "accepted", "whitelisted", "blocked", "blacklisted"]) {
    if (group in report) createList(report, group);
    scriptsCount += report[group].length;
  }

  for (let b of document.querySelectorAll(`.forget, .whitelist, .blacklist`)) {
    b.disabled = false;
  }
  for (let b of document.querySelectorAll(
    `.unknown .forget, .accepted .forget, .blocked .forget,
     .whitelisted .whitelist, .blacklisted .blacklist`
  )) {
    b.disabled = true;
  }

  if (siteStatus && siteStatus !== "unknown") {
    let siteContainer = document.querySelector("#site");
    let statusLabel = siteStatus;
    if (listedSite && listedSite !== report.site) {
      statusLabel += ` via ${listedSite}`;
      siteContainer.querySelector(".forget").disabled = true;
    }
    let status = siteContainer.querySelector(".status");
    status.classList.add(siteStatus);
    status.textContent = statusLabel;
  } else {
    document.querySelector("#site .status").textContent = "";
  }

  let noscript = scriptsCount === 0;
  document.body.classList.toggle("empty", noscript);
}

myPort.onMessage.addListener(m => {
  if (m.show_info) {
    refreshUI(m.show_info);
  }
});

function print_local_storage() {
  myPort.postMessage({ "printlocalstorage": true });
}
function delete_local_storage() {
  myPort.postMessage({ "deletelocalstorage": true });
}