Greasy Fork

Greasy Fork is available in English.

sobclear

添加一个一键已读按钮

当前为 2022-02-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         sobclear
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  添加一个一键已读按钮
// @author       You
// @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));

}

//创建节点
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)







})();