byr-signature

为北邮人论坛发帖添加个性签名

当前为 2016-12-04 提交的版本,查看 最新版本

  1. /* eslint-env greasemonkey, browser */
  2. // ==UserScript==
  3. // @name byr-signature
  4. // @namespace weibo.com/flowmemo
  5. // @version 0.2.0
  6. // @description 为北邮人论坛发帖添加个性签名
  7. // @author flowmemo
  8. // @match *://bbs.byr.cn/*
  9. // @match *://bbs6.byr.cn/*
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @license MIT
  13. // @supportURL https://github.com/flowmemo/byr-signature
  14. // ==/UserScript==
  15.  
  16. ; (function () {
  17. 'use strict'
  18. const _DEBUG = false
  19. const log = (function () {
  20. if (_DEBUG) return console.log.bind(console)
  21. else return Function.prototype
  22. })()
  23.  
  24. log('init')
  25. const added = new WeakSet()
  26.  
  27. const defaultSig =
  28. `————
  29. 示例签名: 微博 [url=http://weibo.com/flowmemo][color=#0000FF]@flowmemo[/color][/url] , 现在主要写JavaScript. 关注广泛, 欢迎交流.
  30. 修改签名请到[url=https://bbs.byr.cn/#!article/WWWTechnology/post][color=#0000FF]这里[/color][/url]
  31. [url=https://github.com/flowmemo/byr-signature][color=#0000FF]此签名通过「北邮人签名档」脚本发送[/color][/url]`
  32.  
  33. function getUsername () {
  34. const user = document.querySelector('.u-login-id a')
  35. if (!user) {
  36. log('cannot find username')
  37. return false
  38. }
  39. const username = user.title
  40. return username
  41. }
  42.  
  43. function getSig () {
  44. const username = getUsername()
  45. if (!username) return false
  46. const signature = GM_getValue('sig-' + username, defaultSig)
  47. return signature
  48. }
  49.  
  50. function addPostSig () {
  51. log('addPostSig')
  52. const div = document.getElementById('post_content')
  53. if (!div || added.has(div)) return false
  54. added.add(div)
  55. div.value += '\n' + getSig()
  56. addSigPanel()
  57. return true
  58. }
  59.  
  60. function addQuickSig () {
  61. log('addQuickSig')
  62. const div = document.getElementsByName('content')[0]
  63. if (!div || added.has(div)) return false
  64. added.add(div)
  65. div.value += '\n' + getSig()
  66. log('add value')
  67. return true
  68. }
  69.  
  70. function addSigPanel () {
  71. log('sigPanel')
  72. const div = document.createElement('div')
  73. const postItems = document.getElementsByClassName('post-list-item')
  74. const referNode = postItems[postItems.length - 2]
  75. referNode.appendChild(div)
  76. div.outerHTML =
  77. `<br><br><div>
  78. <div class="post-m">byr-signature自定义签名</div>
  79. <div class="byr-signature" style="border:1px solid #c9d7f1;padding:5px;width:680px">
  80. <textarea class="post-textarea" name="sig-content" placeholder="在此输入你的签名"></textarea><br>
  81. </div><p><input name="saveSig" type="button" value="保存"> 保存后刷新页面生效(注意保存你的发帖内容) </p></div>`
  82.  
  83. const userSig = document.getElementsByName('sig-content')[0]
  84. userSig.value = getSig()
  85. const saveButton = document.getElementsByName('saveSig')[0]
  86.  
  87. const username = getUsername()
  88. if (!username) return false
  89. saveButton.addEventListener('click', function () {
  90. log('save')
  91. log('username', username)
  92. GM_setValue('sig-' + username, userSig.value)
  93. })
  94. }
  95.  
  96. function onPageChange () {
  97. log('onPageChange')
  98. if (window.location.href.match(/'#!article'/)) return
  99. if (window.location.href.match(/edit|post/)) {
  100. log(addPostSig())
  101. } else log(addQuickSig())
  102. }
  103. onPageChange()
  104. const observer = new MutationObserver(onPageChange)
  105. observer.observe(document.body, {childList: true, subtree: true})
  106. })()