添加一个一键已读按钮
目前為
// ==UserScript==
// @name sobclear
// @namespace http://tampermonkey.net/
// @version 0.3.1
// @description 添加一个一键已读按钮
// @author cctyl
// @match https://www.sunofbeach.net/*
// @icon https://www.google.com/s2/favicons?domain=sunofbeach.net
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
let cssStr = `
#clear-msg{
color: #0084ff;
margin-right: 12px !important;
}
`;
/**
* 添加css样式
* @returns {boolean}
*/
function initCss() {
let addTo = document.querySelector('body');
if (!addTo)
addTo = (document.head || document.body || document.documentElement);
//创建style标签
let cssNode = document.createElement("style");
cssNode.setAttribute("type", "text/css");
//设置css值
cssNode.innerHTML = cssStr;
try {
addTo.appendChild(cssNode);
} catch (e) {
console.log(e.message);
}
}
/**
* 获取cookie值
* @param name
* @returns {string|null}
*/
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
/**
* 发送请求清除消息
*/
function clearMsg() {
// https://api.sunofbeaches.com/ct/msg/read
fetch('https://api.sunofbeaches.com/ct/msg/read',{
headers:{
'sob_token':getCookie('sob_token')
}
})
.then(response => {
console.log(response)
let ring =document.querySelector('.el-badge__content')
ring.style.display="none"
//alert("已读成功")
})
.then(data => console.log(data));
}
/**
* 摸鱼动态的输入框允许粘贴
*/
function allowPaste(){
document.querySelector('div[contenteditable]').addEventListener("paste", function(e) {
e.stopPropagation();
e.preventDefault();
var text = '', event = (e.originalEvent || e);
console.log(event.clipboardData)
if (event.clipboardData && event.clipboardData.getData) {
text = event.clipboardData.getData('text/plain');
} else if (window.clipboardData && window.clipboardData.getData) {
text = window.clipboardData.getData('Text');
}
if (document.queryCommandSupported('insertText')) {
document.execCommand('insertText', false, text);
} else {
document.execCommand('paste', false, text);
}
})
}
//允许粘贴
allowPaste();
//创建节点
let clsBtnParent = document.getElementById('header-login-success');
let newnode = document.createElement("i");
//设置id
newnode.setAttribute('id', 'clear-msg')
newnode.setAttribute('class', 'el-icon-delete')
//添加到节点中
clsBtnParent.insertBefore(newnode, clsBtnParent.firstChild);
//初始化样式
initCss();
//添加点击事件
setTimeout(() => {
newnode.onclick = function () {
clearMsg()
}
}, 1000)
})();