sobclear

添加一个一键已读按钮

  1. // ==UserScript==
  2. // @name sobclear
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.5
  5. // @description 添加一个一键已读按钮
  6. // @author cctyl
  7. // @match https://www.sunofbeach.net/*
  8. // @icon https://www.google.com/s2/favicons?domain=sunofbeach.net
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16.  
  17. let cssStr = `
  18. #clear-msg{
  19. color: #0084ff;
  20. width:15px;
  21. margin-right: 12px !important;
  22. }
  23. `;
  24.  
  25. /**
  26. * 添加css样式
  27. * @returns {boolean}
  28. */
  29. function initCss() {
  30.  
  31. let addTo = document.querySelector('body');
  32. if (!addTo)
  33. addTo = (document.head || document.body || document.documentElement);
  34.  
  35.  
  36. //创建style标签
  37. let cssNode = document.createElement("style");
  38. cssNode.setAttribute("type", "text/css");
  39.  
  40.  
  41. //设置css值
  42. cssNode.innerHTML = cssStr;
  43. try {
  44. addTo.appendChild(cssNode);
  45. } catch (e) {
  46. console.log(e.message);
  47. }
  48.  
  49.  
  50. }
  51.  
  52. /**
  53. * 获取cookie值
  54. * @param name
  55. * @returns {string|null}
  56. * @deprecated 因为有多个sobtoken,暂时不用他
  57. */
  58. function getCookie(name) {
  59. var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  60. if (arr = document.cookie.match(reg))
  61. return unescape(arr[2]);
  62. else
  63. return null;
  64. }
  65.  
  66. //重试次数
  67. let tryCount = 0
  68. //多个sobtoken组成的数组
  69. let sobTokenArr = []
  70.  
  71. /**
  72. * 获取到所有的sobtoken
  73. */
  74. function getSobToken() {
  75. let cookies = document.cookie
  76. cookies = cookies.split(';')
  77. for (let i = 0; i < cookies.length; i++) {
  78.  
  79. let cookieTemp = cookies[i];
  80. if (cookieTemp.indexOf("sob_token") !== -1) {
  81. let tempSobArr = cookieTemp.split('=');
  82. sobTokenArr.push(tempSobArr[1])
  83. }
  84.  
  85. }
  86. }
  87.  
  88.  
  89. /**
  90. * 发送请求清除消息
  91. */
  92. function clearMsg(sobToken) {
  93. // https://api.sunofbeaches.com/ct/msg/read
  94. tryCount++;
  95. fetch('https://api.sunofbeaches.com/ct/msg/read', {
  96. headers: {
  97. 'sob_token': sobToken,
  98.  
  99. },
  100. credentials: "include"
  101. })
  102. .then(response => {
  103.  
  104. response.json().then(value => {
  105. console.log(value)
  106. if (value.code === 10000) {
  107. console.log("发送成功")
  108. let ring = document.querySelector('.el-badge__content')
  109. ring.style.display = "none"
  110.  
  111. } else {
  112. console.log('发送失败')
  113. //如果失败了,就重发
  114. //重发次数有限制,有多少sobtoken就发多少次
  115. if (tryCount < sobTokenArr.length) {
  116. console.log("重试")
  117. clearMsg(sobTokenArr[tryCount])
  118. }
  119. }
  120. })
  121.  
  122.  
  123. })
  124. .then(data => console.log(data));
  125.  
  126. }
  127.  
  128.  
  129. /**
  130. * 摸鱼动态的输入框允许粘贴
  131. */
  132. function allowPaste() {
  133. let divInput = document.querySelector('div[contenteditable]')
  134. if (divInput) {
  135. divInput.addEventListener("paste", function (e) {
  136.  
  137.  
  138. e.stopPropagation();
  139.  
  140. e.preventDefault();
  141.  
  142. var text = '', event = (e.originalEvent || e);
  143.  
  144. console.log(event.clipboardData)
  145.  
  146. if (event.clipboardData && event.clipboardData.getData) {
  147.  
  148. text = event.clipboardData.getData('text/plain');
  149.  
  150. } else if (window.clipboardData && window.clipboardData.getData) {
  151.  
  152. text = window.clipboardData.getData('Text');
  153.  
  154. }
  155.  
  156. if (document.queryCommandSupported('insertText')) {
  157.  
  158. document.execCommand('insertText', false, text);
  159.  
  160. } else {
  161.  
  162. document.execCommand('paste', false, text);
  163.  
  164. }
  165.  
  166. })
  167. }
  168.  
  169.  
  170. }
  171.  
  172.  
  173. //允许粘贴
  174. allowPaste();
  175.  
  176.  
  177. //创建节点
  178. let clsBtnParent = document.getElementById('header-login-success');
  179. let newnode = document.createElement("i");
  180.  
  181.  
  182. //设置id
  183. newnode.setAttribute('id', 'clear-msg')
  184. newnode.setAttribute('class', 'el-icon-delete')
  185.  
  186.  
  187. //添加到节点中
  188. clsBtnParent.insertBefore(newnode, clsBtnParent.firstChild);
  189.  
  190.  
  191. //初始化样式
  192. initCss();
  193.  
  194. //获取cookie
  195. getSobToken();
  196.  
  197. //添加点击事件
  198. setTimeout(() => {
  199. newnode.onclick = function () {
  200.  
  201. clearMsg(sobTokenArr[0])
  202. }
  203. }, 1000)
  204.  
  205.  
  206. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址