I'm wenku8 admin!

通过简单的几行代码把用户伪装成wenku8管管

Dit script moet niet direct worden geïnstalleerd - het is een bibliotheek voor andere scripts om op te nemen met de meta-richtlijn // @require https://update.greasyfork.org/scripts/428403/1000865/I%27m%20wenku8%20admin%21.js

(function () {
	const targetPage = 'https://www.wenku8.net/userinfo.php?id=917768';
	const scriptId = 'pretend_admin';
	const imgId = 'pretend_admin_img';
	const scriptLink = 'https://greasyfork.org/scripts/428403/code/script.js';
	main();

	function main() {
		const img = document.querySelector('#' + imgId);
		img && img.parentElement.removeChild(img);

		switch(location.href) {
			case 'about:blank':
				console.log('Wenku8 admin: Injecting to opener...');
				// Opened page: Inject to opener
				const oDoc = window.opener.document;
				const script = oDoc.querySelector(scriptId);
				loadJS(script.src, null, oDoc);
			case targetPage:
				// Just this page
				console.log('Wenku8 admin: Editing page...');
				getDocument('https://www.wenku8.net/userinfo.php?id=2', function (oDoc) {
					const content = document.querySelector('#content');
					const table = content.querySelector('table');
					const oContent = oDoc.querySelector('#content');
					const otable = oContent.querySelector('table');
					content.innerHTML = oContent.innerHTML;
				});
			default:
				// Opener page: Make all opener links magical
				console.log('Wenku8 admin: Doing magic...');
				if (window.magical) {
					return false;
				}
				window.magical = true;
				const as = document.querySelectorAll('a[href="https://www.wenku8.net/userinfo.php?id=917768"]');
				for (const a of as) {
					a.addEventListener('click', function (e) {
						destroyEvent(e);
						const newtab = window.open(a.href);
						const oWin = newtab.window;
						const oDom = newtab.document;
						oWin.addEventListener('load', function(e) {
							const win = e.currentTarget;
							const dom = win.document;
							const script = dom.createElement('script');
							script.src = scriptLink;
							dom.head.appendChild(script);
						});
					});
				}
		}
		return true;
	}

	// Load javascript from given url
	function loadJS(url, callback, oDoc = document) {
		var script = document.createElement('script'),
			fn = callback || function () {};
		script.type = 'text/javascript';
		//IE
		if (script.readyState) {
			script.onreadystatechange = function () {
				if (script.readyState == 'loaded' || script.readyState == 'complete') {
					script.onreadystatechange = null;
					fn();
				}
			};
		} else {
			//其他浏览器
			script.onload = function () {
				fn();
			};
		}
		script.src = url;
		oDoc.getElementsByTagName('head')[0].appendChild(script);
	}

	// Download and parse a url page into a html document(dom).
	// when xhr onload: callback.apply([dom, args])
	function getDocument(url, callback, args = []) {
		const xhr = new XMLHttpRequest();
		xhr.open('GET', url, true);
		xhr.responseType = 'blob';
		xhr.onload = function (response) {
			const htmlblob = xhr.response;
			const reader = new FileReader();
			reader.onload = function (e) {
				const htmlText = reader.result;
				const dom = new DOMParser().parseFromString(htmlText, 'text/html');
				args = [dom].concat(args);
				callback.apply(null, args);
				//callback(dom, htmlText);
			}
			reader.readAsText(htmlblob, 'GBK');
		}
		xhr.send();
	}

	// Just stopPropagation and preventDefault
	function destroyEvent(e) {
		if (!e) {return false;};
		if (!e instanceof Event) {return false;};
		e.stopPropagation();
		e.preventDefault();
	}
}) ();