disableThirdpartyAD

禁止手机站点的第三方资源,当然你也可加入其它站点

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

Advertisement:

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

Advertisement:

// ==UserScript==
// @name           disableThirdpartyAD
// @namespace      disableThirdpartyAD.xinggsf
// @author	       xinggsf
// @description    禁止手机站点的第三方资源,当然你也可加入其它站点
// downloadUrl     https://raw.githubusercontent.com/xinggsf/gm/master/disableThirdpartyAD.user.js
// @include        http://m.*
// @include        http://wap.*
// @include        https://m.*
// @include        https://wap.*
// @version        2016.9.26
// @encoding       utf-8
// @run-at         document-start
// @grant          unsafeWindow
// ==/UserScript==
"use strict";
if (unsafeWindow.top === unsafeWindow.self) {
	let baseHost = '??',
	urlWhiteList = [],
	regIp = /(?:\d{1,3}\.){3}\d{1,3}/,
	r = /:\/\/([^:\/]+)/,
	getBaseHost = function (url) {//取主域名
		let m = url.match(r);
		if (!m || m[1].includes(baseHost))
			return baseHost;
		m = m[1];
		let a = m.split('.');
		if (a.length === 2)
			return m;
		let i, s, ret = a.pop();
		for (i = 0; i < 2; i++) {
			s = a.pop();
			ret = s + '.' + ret;
			//广告主域名一般超过3字符
			if (!/[a-z]{2,3}/i.test(s))
				return ret;
		}
		return ret;
	},
	delAdNode = function (e) {
		switch (e.tagName) {
		case 'SCRIPT':
		case 'IFRAME':
			if (isThirdparty(e.getAttribute('src')))
				e.parentNode.removeChild(e);
			break;
			/* case  'IMAGE':
			case  'VIDEO':

			case  'OBJECT':
			case  'EMBED': */
		}
	},
	isThirdparty = function (url) {
		if (!url || urlWhiteList.includes(url))
			return !1;
		return baseHost !== getBaseHost(url);
	};

	baseHost = regIp.test(location.hostname) ?
		location.hostname : getBaseHost(location.hostname);
	let mo = new MutationObserver(function (rs) {
		for (let col of rs)
			for (let e of col.addedNodes)
				delAdNode(e);
	});
	mo.observe(document, {
		childList : true,
		subtree : true
	});
}