term.ptt.cc 自動登入

自動登入 term.ptt.cc + 自動跳過一些畫面

当前为 2018-05-25 提交的版本,查看 最新版本

// ==UserScript==
// @name         term.ptt.cc 自動登入
// @namespace    https://blog.maple3142.net/
// @version      0.2.1
// @description  自動登入 term.ptt.cc + 自動跳過一些畫面
// @author       maple3142
// @match        https://term.ptt.cc/
// @run-at       document-start
// @grant        unsafeWindow
// @grant        window.close
// ==/UserScript==

const ID = ''
const PASS = ''
const skiplist = [
	{ re: /歡迎您再度拜訪,上次您是從.*連往本站。/, input: '\n' },
	{ re: /單一小時上線人次.*單日上線人次/, input: 'q' },
    { re: /您想刪除其他重複登入的連線嗎?\[Y\/n\]/, input: 'y\n' }
]
const closeOnQuit = true
;(function() {
	'use strict'
	// event
	const E = {
		callbacks: {},
		on(evt, cb) {
			if (!Array.isArray(this.callbacks[evt])) this.callbacks[evt] = []
			this.callbacks[evt].push(cb)
		},
		trigger(evt, ...args) {
			if (!Array.isArray(this.callbacks[evt])) return
			this.callbacks[evt].forEach(cb => cb(...args))
		}
	}

	// helpers
	const insertText = (() => {
		let t = document.querySelector('#t')
		return str => {
			if (!t) t = document.querySelector('#t')
			const e = new CustomEvent('paste')
			e.clipboardData = { getData: () => str }
			t.dispatchEvent(e)
		}
	})()
	function hook(obj, key, cb) {
		const fn = obj[key].bind(obj)
		obj[key] = function(...args) {
			fn.apply(this, args)
			cb.apply(this, args)
		}
	}

	// console hook
	hook(unsafeWindow.console, 'info', t => {
		if (typeof t !== 'string' || !/pttchrome (\w+)/.test(t)) return
		const evt = /pttchrome (\w+)/.exec(t.trim())[1]
		E.trigger(evt.replace(/^on/, '').toLowerCase())
	})
	hook(unsafeWindow.console, 'log', t => {
		if (t === 'view update') E.trigger('update')
	})

	// main
	E.on('connect', () => insertText(ID + '\n' + PASS + '\n'))
	E.on('update', () => {
		const t = $('span[type=bbsRow]').text()
		skiplist.filter(x => x.re.test(t)).forEach(x => insertText(x.input))
	})
	if (closeOnQuit) E.on('close', close)
})()