term.ptt.cc 自動登入

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

  1. // ==UserScript==
  2. // @name term.ptt.cc 自動登入
  3. // @namespace https://blog.maple3142.net/
  4. // @version 0.3.0
  5. // @description 自動登入 term.ptt.cc + 自動跳過一些畫面
  6. // @author maple3142
  7. // @match https://term.ptt.cc/
  8. // @run-at document-start
  9. // @grant unsafeWindow
  10. // @grant window.close
  11. // @compatible firefox >=52
  12. // @compatible chrome >=55
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. const ID = ''
  17. const PASS = ''
  18. const skiplist = [
  19. { re: /歡迎您再度拜訪,上次您是從.*連往本站。/, input: '\n' }, //跳過啟動頁 1
  20. { re: /單一小時上線人次.*單日上線人次/, input: 'q' }, // 跳過啟動頁 2
  21. { re: /您想刪除其他重複登入的連線嗎?\[Y\/n\]/, input: 'y\n' }, // 自動刪除重複登入
  22. { re: /上方為使用者心情點播留言區,不代表本站立場/, input: 'f\n' } // 自動進入我的最愛(第一次)
  23. ]
  24. const closeOnQuit = true
  25. const closeWarning = true
  26. ;(function() {
  27. 'use strict'
  28. // event
  29. const E = {
  30. callbacks: {},
  31. on(evt, cb) {
  32. if (!Array.isArray(this.callbacks[evt])) this.callbacks[evt] = []
  33. this.callbacks[evt].push(cb)
  34. },
  35. emit(evt, ...args) {
  36. if (!Array.isArray(this.callbacks[evt])) return
  37. this.callbacks[evt].forEach(cb => cb(...args))
  38. },
  39. off(evt, cb) {
  40. if (!cb) this.callbacks[evt] = []
  41. else this.callbacks = this.callbacks.filter(x => x !== cb)
  42. }
  43. }
  44.  
  45. // helpers
  46. const insertText = (() => {
  47. let t = document.querySelector('#t')
  48. return str => {
  49. if (!t) t = document.querySelector('#t')
  50. const e = new CustomEvent('paste')
  51. e.clipboardData = { getData: () => str }
  52. t.dispatchEvent(e)
  53. }
  54. })()
  55. function hook(obj, key, cb) {
  56. const fn = obj[key].bind(obj)
  57. obj[key] = function(...args) {
  58. fn.apply(this, args)
  59. cb.apply(this, args)
  60. }
  61. }
  62.  
  63. // console hook
  64. hook(unsafeWindow.console, 'info', t => {
  65. if (typeof t !== 'string' || !/pttchrome (\w+)/.test(t)) return
  66. const evt = /pttchrome (\w+)/.exec(t.trim())[1]
  67. E.emit(evt.replace(/^on/, '').toLowerCase())
  68. })
  69. hook(unsafeWindow.console, 'log', t => {
  70. if (t === 'view update') E.emit('update')
  71. })
  72.  
  73. // main
  74. E.on('connect', () => insertText(ID + '\n' + PASS + '\n'))
  75. E.on('update', () => {
  76. const t = Array.prototype.map.call(document.querySelectorAll('span[type=bbsRow]'), x => x.textContent).join('')
  77. const ar = skiplist.filter(x => x.re.test(t))
  78. for (let i = 0; i < ar.length; i++) {
  79. if (ar[i].executed) continue
  80. insertText(ar[i].input)
  81. ar[i].executed = true
  82. }
  83. if (skiplist.filter(x => x.executed).length === skiplist.length) E.off('update')
  84. })
  85. if (closeOnQuit) E.on('close', close)
  86. if (!closeWarning) {
  87. unsafeWindow.addEventListener = (fn => (...args) => {
  88. if (args[0] === 'beforeunload') return
  89. fn(...args)
  90. })(unsafeWindow.addEventListener.bind(unsafeWindow))
  91. }
  92. })()