Hijack Worker

Hijack web workers in the browser to stop coin miner

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name Hijack Worker
// @version 0.2.0
// @description Hijack web workers in the browser to stop coin miner
// @homepageURL https://github.com/eight04/hijack-worker
// @supportURL https://github.com/eight04/hijack-worker/issues
// @license MIT
// @author eight04 <[email protected]>
// @include *
// @namespace eight04.blogspot.com
// @require https://greasyfork.org/scripts/7212-gm-config-eight-s-version/code/GM_config%20(eight's%20version).js?version=156587
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant unsafeWindow
// @run-at document-start
// ==/UserScript==

/* global GM_config */

let blacklist = [];
let whitelist = [];

GM_config.setup({
	whitelist: {
		label: "Allowed URLs (regexp per line)",
		type: "textarea",
		default: ""
	},
	blacklist: {
		label: "Restricted URLs (regexp per line)",
		type: "textarea",
		default: ""
	}
}, () => {
	const config = GM_config.get();
	whitelist = getRxList(config.whitelist);
	blacklist = getRxList(config.blacklist);
});

function getRxList(text) {
  return text.trim().split(/\s*\n\s*/).filter(Boolean).map(p => new RegExp(p, "i"));
}

unsafeWindow.Worker = (Worker => {
	return class extends Worker {
		constructor(url, ...args) {
			if (!valid(url)) {
				throw new Error(`Worker is not allowed: ${url}`);
			}
      super(url, ...args);
		}
	};
})(unsafeWindow.Worker);

unsafeWindow.SharedWorker = (SharedWorker => {
	return class extends SharedWorker {
		constructor(url, ...args) {
			if (!valid(url)) {
				throw new Error(`SharedWorker is not allowed: ${url}`);
			}
      super(url, ...args);
		}
	};
})(unsafeWindow.SharedWorker);

const tempBlacklist = new Set;
const tempWhitelist = new Set;

function valid(url) {
  if (tempWhitelist.has(url)) {
    return true;
  }
  if (tempBlacklist.has(url)) {
    return false;
  }
  for (const rx of whitelist) {
    if (rx.test(url)) {
      tempWhitelist.add(url);
      return true;
    }
  }
  for (const rx of blacklist) {
    if (rx.test(url)) {
      tempBlacklist.add(url);
      return false;
    }
  }
  const message = `Do you want to allow web worker?
- from: ${location.href}
- target: ${url}`;
  console.log(message); // eslint-disable-line no-console
  if (confirm(message)) {
    tempWhitelist.add(url);
    return true;
  }
  tempBlacklist.add(url);
  return false;
}