aboutsummaryrefslogtreecommitdiff
path: root/bg
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-04-06 17:35:05 +1000
committerYuchen Pei <hi@ypei.me>2022-04-07 12:18:45 +1000
commit34bcc1a5c2750b6f6fa9d9b971ac8aff796ddd1c (patch)
tree8e2a4e27fd168c45bb9a5157e172822258417d13 /bg
parent2e10129fa088584af25dcb34834551380e8f4521 (diff)
linting
- eslint - also adding eslintrc
Diffstat (limited to 'bg')
-rw-r--r--bg/ExternalLicenses.js29
-rw-r--r--bg/ListManager.js20
-rw-r--r--bg/ResponseMetaData.js18
-rw-r--r--bg/ResponseProcessor.js32
4 files changed, 50 insertions, 49 deletions
diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js
index 0e09b6d..1a8e58c 100644
--- a/bg/ExternalLicenses.js
+++ b/bg/ExternalLicenses.js
@@ -23,12 +23,12 @@
Singleton to handle external licenses, e.g. WebLabels
*/
-"use strict";
+'use strict';
let licensesByLabel = new Map();
let licensesByUrl = new Map();
{
- let { licenses } = require("../license_definitions");
+ let { licenses } = require('../license_definitions');
let mapByLabel = (label, license) => licensesByLabel.set(label.toUpperCase(), license);
for (let [id, l] of Object.entries(licenses)) {
let { identifier, canonicalUrl, licenseName } = l;
@@ -64,7 +64,7 @@ var ExternalLicenses = {
let frameCache = tabCache && tabCache.get(frameId);
let cache = frameCache && frameCache.get(documentUrl);
let scriptInfo = await browser.tabs.sendMessage(tabId, {
- action: "checkLicensedScript",
+ action: 'checkLicensedScript',
url,
cache,
}, { frameId });
@@ -74,10 +74,10 @@ var ExternalLicenses = {
}
scriptInfo.licenses = new Set();
scriptInfo.toString = function() {
- let licenseIds = [...this.licenses].map(l => l.identifier).sort().join(", ");
+ let licenseIds = [...this.licenses].map(l => l.identifier).sort().join(', ');
return licenseIds
- ? `Free license${this.licenses.size > 1 ? "s" : ""} (${licenseIds})`
- : "Unknown license(s)";
+ ? `Free license${this.licenses.size > 1 ? 's' : ''} (${licenseIds})`
+ : 'Unknown license(s)';
}
let match = (map, key) => {
if (map.has(key)) {
@@ -112,22 +112,23 @@ var ExternalLicenses = {
}
frameCache.set(frameId, new Map([[documentUrl, cache]]));
- let link = doc.querySelector(`link[rel="jslicense"], link[data-jslicense="1"], a[rel="jslicense"], a[data-jslicense="1"]`);
+ let link = doc.querySelector('link[rel="jslicense"], link[data-jslicense="1"], a[rel="jslicense"], a[data-jslicense="1"]');
if (link) {
- let href = link.getAttribute("href");
+ let href = link.getAttribute('href');
cache.webLabels = { href };
let move = () => !!doc.head.insertBefore(link, doc.head.firstChild);
if (link.parentNode === doc.head) {
- for (let node = link; node = node.previousElementSibling;) {
- if (node.tagName.toUpperCase() === "SCRIPT") {
+ let node = link.previousElementSibling;
+ for (; node; node = node.previousElementSibling) {
+ if (node.tagName.toUpperCase() === 'SCRIPT') {
return move();
}
}
} else { // the reference is only in the body
- if (link.tagName.toUpperCase() === "A") {
- let newLink = doc.createElement("link");
- newLink.rel = "jslicense";
- newLink.setAttribute("href", href);
+ if (link.tagName.toUpperCase() === 'A') {
+ let newLink = doc.createElement('link');
+ newLink.rel = 'jslicense';
+ newLink.setAttribute('href', href);
link = newLink;
}
return move();
diff --git a/bg/ListManager.js b/bg/ListManager.js
index f712356..3020197 100644
--- a/bg/ListManager.js
+++ b/bg/ListManager.js
@@ -23,7 +23,7 @@
A class to manage whitelist/blacklist operations
*/
-let { ListStore } = require("../common/Storage");
+let { ListStore } = require('../common/Storage');
class ListManager {
constructor(whitelist, blacklist, builtInHashes) {
@@ -48,13 +48,13 @@ class ListManager {
with a trailing (hash).
Returns "blacklisted", "whitelisted" or defValue
*/
- getStatus(key, defValue = "unknown") {
+ getStatus(key, defValue = 'unknown') {
let { blacklist, whitelist } = this.lists;
let inline = ListStore.inlineItem(key);
if (inline) {
return blacklist.contains(inline)
- ? "blacklisted"
- : whitelist.contains(inline) ? "whitelisted"
+ ? 'blacklisted'
+ : whitelist.contains(inline) ? 'whitelisted'
: defValue;
}
@@ -63,16 +63,16 @@ class ListManager {
let url = ListStore.urlItem(key);
let site = ListStore.siteItem(key);
return (blacklist.contains(url) || ListManager.siteMatch(site, blacklist)
- ? "blacklisted"
+ ? 'blacklisted'
: whitelist.contains(url) || ListManager.siteMatch(site, whitelist)
- ? "whitelisted" : defValue
+ ? 'whitelisted' : defValue
);
}
let [hashItem, srcHash] = match; // (hash), hash
- return blacklist.contains(hashItem) ? "blacklisted"
+ return blacklist.contains(hashItem) ? 'blacklisted'
: this.builtInHashes.has(srcHash) || whitelist.contains(hashItem)
- ? "whitelisted"
+ ? 'whitelisted'
: defValue;
}
@@ -85,13 +85,13 @@ class ListManager {
if (list.contains(site)) {
return site;
}
- site = site.replace(/^([\w-]+:\/\/)?(\w)/, "$1*.$2");
+ site = site.replace(/^([\w-]+:\/\/)?(\w)/, '$1*.$2');
for (; ;) {
if (list.contains(site)) {
return site;
}
let oldKey = site;
- site = site.replace(/(?:\*\.)*\w+(?=\.)/, "*");
+ site = site.replace(/(?:\*\.)*\w+(?=\.)/, '*');
if (site === oldKey) {
return null;
}
diff --git a/bg/ResponseMetaData.js b/bg/ResponseMetaData.js
index 4570120..cb3fb2a 100644
--- a/bg/ResponseMetaData.js
+++ b/bg/ResponseMetaData.js
@@ -34,24 +34,24 @@ class ResponseMetaData {
this.headers = {};
for (let h of responseHeaders) {
if (/^\s*Content-(Type|Disposition)\s*$/i.test(h.name)) {
- let propertyName = h.name.split("-")[1].trim();
+ let propertyName = h.name.split('-')[1].trim();
propertyName = `content${propertyName.charAt(0).toUpperCase()}${propertyName.substring(1).toLowerCase()}`;
this[propertyName] = h.value;
this.headers[propertyName] = h;
}
}
- this.computedCharset = "";
+ this.computedCharset = '';
}
get charset() {
- let charset = "";
+ let charset = '';
if (this.contentType) {
let m = this.contentType.match(/;\s*charset\s*=\s*(\S+)/);
if (m) {
charset = m[1];
}
}
- Object.defineProperty(this, "charset", { value: charset, writable: false, configurable: true });
+ Object.defineProperty(this, 'charset', { value: charset, writable: false, configurable: true });
return this.computedCharset = charset;
}
@@ -69,12 +69,12 @@ class ResponseMetaData {
// let's try figuring out the charset from <meta> tags
let parser = new DOMParser();
- let doc = parser.parseFromString(text, "text/html");
+ let doc = parser.parseFromString(text, 'text/html');
let meta = doc.querySelectorAll('meta[charset], meta[http-equiv="content-type"], meta[content*="charset"]');
for (let m of meta) {
- charset = m.getAttribute("charset");
+ charset = m.getAttribute('charset');
if (!charset) {
- let match = m.getAttribute("content").match(/;\s*charset\s*=\s*([\w-]+)/i)
+ let match = m.getAttribute('content').match(/;\s*charset\s*=\s*([\w-]+)/i)
if (match) charset = match[1];
}
if (charset) {
@@ -89,7 +89,7 @@ class ResponseMetaData {
return text;
}
- createDecoder(charset = this.charset, def = "latin1") {
+ createDecoder(charset = this.charset, def = 'latin1') {
if (charset) {
try {
return new TextDecoder(charset);
@@ -99,7 +99,7 @@ class ResponseMetaData {
}
return def ? new TextDecoder(def) : null;
}
-};
+}
ResponseMetaData.UTF8BOM = new Uint8Array(BOM);
module.exports = { ResponseMetaData };
diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js
index 078f38a..d964dab 100644
--- a/bg/ResponseProcessor.js
+++ b/bg/ResponseProcessor.js
@@ -24,22 +24,22 @@
only the "interesting" HTML and script requests and leaving the other alone
*/
-let { ResponseMetaData } = require("./ResponseMetaData");
+let { ResponseMetaData } = require('./ResponseMetaData');
let listeners = new WeakMap();
let webRequestEvent = browser.webRequest.onHeadersReceived;
class ResponseProcessor {
- static install(handler, types = ["main_frame", "sub_frame", "script"]) {
+ static install(handler, types = ['main_frame', 'sub_frame', 'script']) {
if (listeners.has(handler)) return false;
let listener =
async request => await new ResponseTextFilter(request).process(handler);
listeners.set(handler, listener);
webRequestEvent.addListener(
listener,
- { urls: ["<all_urls>"], types },
- ["blocking", "responseHeaders"]
+ { urls: ['<all_urls>'], types },
+ ['blocking', 'responseHeaders']
);
return true;
}
@@ -67,21 +67,21 @@ class ResponseTextFilter {
this.canProcess = // we want to process html documents and scripts only
(statusCode < 300 || statusCode >= 400) && // skip redirections
!md.disposition && // skip forced downloads
- (type === "script" || /\bhtml\b/i.test(md.contentType));
+ (type === 'script' || /\bhtml\b/i.test(md.contentType));
}
async process(handler) {
if (!this.canProcess) return ResponseProcessor.ACCEPT;
let { metaData, request } = this;
let response = { request, metaData }; // we keep it around allowing callbacks to store state
- if (typeof handler.pre === "function") {
+ if (typeof handler.pre === 'function') {
let res = await handler.pre(response);
if (res) return res;
if (handler.post) handler = handler.post;
- if (typeof handler !== "function") return ResponseProcessor.ACCEPT;
+ if (typeof handler !== 'function') return ResponseProcessor.ACCEPT;
}
- let { requestId, responseHeaders } = request;
+ let { requestId } = request;
let filter = browser.webRequest.filterResponseData(requestId);
let buffer = [];
@@ -89,9 +89,9 @@ class ResponseTextFilter {
buffer.push(event.data);
};
- filter.onstop = async event => {
+ filter.onstop = async unused => {
// concatenate chunks
- let size = buffer.reduce((sum, chunk, n) => sum + chunk.byteLength, 0)
+ let size = buffer.reduce((sum, chunk) => sum + chunk.byteLength, 0)
let allBytes = new Uint8Array(size);
let pos = 0;
for (let chunk of buffer) {
@@ -100,13 +100,13 @@ class ResponseTextFilter {
}
buffer = null; // allow garbage collection
if (allBytes.indexOf(0) !== -1) {
- console.debug("Warning: zeroes in bytestream, probable cached encoding mismatch.", request);
- if (request.type === "script") {
- console.debug("It's a script, trying to refetch it.");
- response.text = await (await fetch(request.url, { cache: "reload", credentials: "include" })).text();
+ console.debug('Warning: zeroes in bytestream, probable cached encoding mismatch.', request);
+ if (request.type === 'script') {
+ console.debug('It\'s a script, trying to refetch it.');
+ response.text = await (await fetch(request.url, { cache: 'reload', credentials: 'include' })).text();
} else {
- console.debug("It's a %s, trying to decode it as UTF-16.", request.type);
- response.text = new TextDecoder("utf-16be").decode(allBytes, { stream: true });
+ console.debug('It\'s a %s, trying to decode it as UTF-16.', request.type);
+ response.text = new TextDecoder('utf-16be').decode(allBytes, { stream: true });
}
} else {
response.text = metaData.decode(allBytes);