Greasy Fork

Greasy Fork is available in English.

pseudo-Benben - tiger0132

qwq

目前为 2020-05-03 提交的版本。查看 最新版本

// ==UserScript==
// @name         pseudo-Benben - tiger0132
// @namespace    request://tampermonkey.net/
// @version      0.4
// @description  qwq
// @author       tiger0132
// @match        https://*.luogu.com.cn/
// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_removeValue
// ==/UserScript==

(function () {
	'use strict';

	const $ = unsafeWindow.$;

	const request = ({ url, data }) => {
		return new Promise((resolve, reject) => {
			unsafeWindow.handleData = data => {
				const keys = Object.keys(data);
				const keysLen = keys.length;
				return keys.reduce((pre, cur, index) => {
					const value = encodeURIComponent(data[cur]);
					const flag = index !== keysLen - 1 ? '&' : '';
					return `${pre}${cur}=${value}${flag}`;
				}, '');
			};
			const script = document.createElement('script');
			unsafeWindow.handleResponse = res => {
				document.body.removeChild(script);
				delete unsafeWindow.handleResponse;
				resolve(res);
			};
			script.src = `${url}?${handleData(data)}`;
			document.body.appendChild(script);
		});
	};
	function verifyToken(token) {
		console.log(token);
		fetch("https://www.luogu.com.cn/paste/new", {
			"headers": {
				"content-type": "application/json;charset=UTF-8",
				"x-csrf-token": document.getElementsByName('csrf-token')[0].content,
			},
			"body": `{"public":true,"data":"${token}"}`,
			"method": "POST",
		}).then(resp => resp.json()).then(resp => {
			request({
				url: 'https://akioi.ml:8204/api/verifyToken',
				data: { pasteId: resp.id }
			}).then(res => {
				if (res.status != 200) {
					console.error(res.data);
					return alert('[Benben\'] 出了一点问题 >_<');
				}
				console.log(`[Benben'] token: ${token}, paste: ${resp.id}`);
				GM_setValue('benben_token', token);
				fetch(`https://www.luogu.com.cn/paste/delete/${resp.id}`, {
					"credentials": "include",
					"headers": {
						"content-type": "application/json;charset=UTF-8",
						"x-csrf-token": document.getElementsByName('csrf-token')[0].content,
					}, "referrer": "https://www.luogu.com.cn/paste/nzqcvntn",
					"referrerPolicy": "no-referrer-when-downgrade",
					"body": `{\"data\":\"${token}\",\"id\":\"${resp.id}\",\"public\":true}`,
					"method": "POST", "mode": "cors"
				}).then(res => res.json()).then(res => {
					if (res.id != resp.id) {
						console.error(`error when deleting paste: ${res.id} != ${resp.id}`);
						return alert('[Benben\'] 出了一点问题 >_<');
					}
					alert('[Benben\'] 验证成功,请刷新');
				});
			});
		});
	}
	function genNewToken() {
		request({
			url: 'https://akioi.ml:8204/api/getToken',
			data: { uid: _feInstance.currentUser.uid }
		}).then(token => {
			if (token.status != 200) {
				console.error(res.data);
				return alert('[Benben\'] 出了一点问题 >_<');
			}
			verifyToken(token.data);
		});
	}

	const token = GM_getValue('benben_token');
	var sendMode = 1; // 1 是伪犇,0 是原
	var origSend;

	function injectPostFeed() {
		origSend = $._data($('#feed-submit')[0], 'events').click[0].handler;

		$('#feed-submit').unbind();
		document.getElementById('feed-submit').onclick = function () {
			console.log(sendMode);
			if (sendMode) {
				this.classList.add('am-disabled');
				var content = document.getElementById('feed-content').value;
				request({
					url: 'https://akioi.ml:8204/api/postFeed',
					data: { content: content, token: token }
				}).then(resp => {
					if (resp.status !== 200) {
						show_alert('好像哪里有点问题', resp.data);
					} else {
						document.getElementById('feed-content').value = '';
						switchMode('pbenben-all');
					}
					this.classList.remove('am-disabled');
				});
			} else
				origSend();
		};
	}

	const selector_html = `<a style="cursor: pointer">伪犇犇</a>`;

	var origLoadFeed = unsafeWindow.loadFeed;
	unsafeWindow.loadFeed = () => {
		if (unsafeWindow.feedMode.indexOf('pbenben') != -1) p_loadFeed();
		else origLoadFeed();
	}
	function p_loadFeed() {
		request({
			url: 'https://akioi.ml:8204/feed/' + unsafeWindow.feedMode.substr(8),
			data: {
				page: unsafeWindow.feedPage,
				uid: _feInstance.currentUser.uid
			}
		}).then(resp => {
			$feed.append(resp.data);
			$('#feed-more').children('a').text('点击查看更多...')
			$('[name=feed-reply]').click(function () {
				var content = $(this).parents('li.feed-li').find('.feed-comment').text();
				$('#feed-content').val(' || @' + $(this).attr('data-username') + ' : ' + content)
			})
		});
		feedPage++;
	}
	function addSelector() {
		var node = document.createElement('li');
		node.className = 'feed-selector';
		node.setAttribute('data-mode', 'pbenben-all');
		node.id = 'pbenben-all';
		node.innerHTML = selector_html;

		document.getElementById('home-center-nav').appendChild(node);
		$('#pbenben-all').click(() => {
			switchMode('pbenben-all');
			sendMode = 1;
		});
	}

	const status_html = `
<h2>伪犇犇</h2>
<span>状态:<span id="pbenben-status">N/A</span>&nbsp;
<button class="am-btn am-btn-primary am-btn-sm" id="benben-btn" style="visibility: hidden !important">验证 token</button></span><br>
<!--<button class="am-btn am-btn-primary am-btn-sm" id="switch-btn">切换到原犇犇</button><br>-->
<small>注:如果选中的是「我关注的」、「全网动态」(和「我发布的」),那么发送的内容就在原犇犇,反之亦然</small>`;
	function init() {
		if (!_feInstance.currentUser) {
			console.error('[Benben\'] Not logined!');
			return;
		}

		var node = document.createElement('div');
		node.className = 'lg-article';
		node.id = 'benben-status';
		node.innerHTML = status_html;

		document.querySelector('div.lg-index-benben > div:nth-child(2)').insertAdjacentElement('afterend', node);
		// document.getElementById('switch-btn').onclick = function () {
		// 	sendMode ^= 1;
		// 	this.textContent = sendMode ? '切换到原犇犇' : '切换到伪犇犇';
		// };

		$('li[data-mode=all],li[data-mode=watching],li[data-mode=my]').click(() => sendMode = 0);

		request({
			url: 'https://akioi.ml:8204/api/tokenStatus',
			data: { token: token }
		}).then(resp => {
			var statusNode = document.getElementById('pbenben-status');
			var btn = document.getElementById('benben-btn');
			statusNode.textContent = '';
			if (resp.status != 200) {
				statusNode.style.color = '#e74c3c';
				statusNode.textContent = `错误:${resp.data}`;
				btn.style.visibility = 'visible';
				btn.onclick = function () {
					this.classList.add('am-disabled');
					genNewToken();
				};
			} else if (resp.data.verified/* && resp.data.uid == _feInstance.currentUser.uid*/) {
				statusNode.style.color = '#5eb95e';
				statusNode.textContent = '正常';
			} else {
				btn.style.visibility = 'visible';
				statusNode.style.color = '#e67e22';
				if (!resp.data.verified) {
					statusNode.textContent = 'token 未验证&nbsp;';
					btn.onclick = function () {
						this.classList.add('am-disabled');
						verifyToken(token);
					};
				}
				// if (resp.data.uid != _feInstance.currentUser.uid) {
				// 	statusNode.style.color = 'red';
				// 	statusNode.textContent += 'uid 不匹配';
				// 	btn.onclick = function () {
				// 		this.classList.add('am-disabled');
				// 		genNewToken();
				// 	};
				// }
			}
		});

		injectPostFeed();
		addSelector();
	}
	init();

	// unsafeWindow.verifyToken = verifyToken;
	unsafeWindow.getToken = () => GM_getValue('benben_token');
	unsafeWindow.setToken = data => GM_setValue('benben_token', data);
	unsafeWindow.rmToken = () => GM_deleteValue('benben_token');

	// console.log(`benben_token: ${GM_getValue('benben_token')}`);
	// if (!GM_getValue('benben_token')) {
	// 	verifyToken();
	// }
})();