微博收藏脚本(A656)

在微博网页端,帖子收藏的成本较高,1)“更多菜单-收藏/取消收藏”的二次点击,2)“收藏/取消收藏”按钮点击后有2秒的全屏弹窗,此时无法进行其它点击操作。编写本脚本的目的是解决问题1,以方便微博的收藏,具体为为每个帖子创建一个收藏按钮,至于问题2,推荐使用Adguard插件添加以下两条规则“s.weibo.com##div.woo-box-flex.woo-box-alignCenter.woo-box-justifyCenter.woo-modal-wrap”、“weibo.com##div.woo-box-flex.woo-box-alignCenter.woo-box-justifyCenter.woo-modal-wrap”。

当前为 2023-03-08 提交的版本,查看 最新版本

// ==UserScript==
// @name         微博收藏脚本(A656)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  在微博网页端,帖子收藏的成本较高,1)“更多菜单-收藏/取消收藏”的二次点击,2)“收藏/取消收藏”按钮点击后有2秒的全屏弹窗,此时无法进行其它点击操作。编写本脚本的目的是解决问题1,以方便微博的收藏,具体为为每个帖子创建一个收藏按钮,至于问题2,推荐使用Adguard插件添加以下两条规则“s.weibo.com##div.woo-box-flex.woo-box-alignCenter.woo-box-justifyCenter.woo-modal-wrap”、“weibo.com##div.woo-box-flex.woo-box-alignCenter.woo-box-justifyCenter.woo-modal-wrap”。
// @author       Fat Cabbage
// @license      MIT
// @match        https://weibo.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=weibo.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    setTimeout(() => {
        let node = document.querySelector('div[class="vue-recycle-scroller__item-wrapper"]');

        node.addEventListener('DOMNodeInserted', ev => {
            checkView(node);
        });

        checkView(node);
    }, 2000)
})();

function checkView(el) {
    el.childNodes.forEach(node => {
        let value = node.getAttribute('data_a656_value1');
        if (value == null || value === false) {
            node.setAttribute('data_a656_value1', true);
            placeButton(node);
        }
    });
}

function placeButton(el = document) {

    let buttonClassName = 'favorite_button_a656'
    let buttonNode = document.createElement('button');
    buttonNode.className = buttonClassName
    buttonNode.innerText = '切换收藏';
    buttonNode.addEventListener('click', ev => {
        let node = ev.target;
        let topNode = node.nextSibling;

        setTimeout(() => {
            topNode.querySelector('i[class*="morepop_action"]').click();

            setTimeout(() => {
                let info;
                topNode.getElementsByClassName('woo-box-flex woo-box-alignCenter woo-pop-item-main').forEach(ev => {
                    if (ev.innerText === '收藏' || ev.innerText === '取消收藏') {
                        info = ev.innerText;
                        ev.click();
                        toast(`已${info}`, 500);
                    }
                });
            }, 10);
        }, 10);
    });

    let targetNode = el.querySelector('div[class^="Feed_body"]').querySelector('div[class*="head_main"]');
    targetNode.parentNode.insertBefore(buttonNode, targetNode.nextSibling);
}

function toast(msg, duration) {
    duration = isNaN(duration) ? 3000 : duration;
    let m = document.createElement('div');
    m.innerHTML = msg;

    m.style.setProperty('font-size', '20px', 'important');
    m.style.setProperty('color', 'rgb(255, 255, 255)', 'important');
    m.style.setProperty('background-color', 'rgba(0,0,0,0.6)', 'important');
    m.style.setProperty('border-style', 'solid', 'important');
    m.style.setProperty('border-color', '#ffffff', 'important');
    m.style.setProperty('z-index', '256', 'important');

    m.style.cssText = 'font-size: 20px; ' +
        'color: rgb(255, 255, 255); ' +
        'background-color: rgba(0,0,0,0.6); ' +
        'border-style: solid; ' +
        'border-color: #ffffff; ' +
        'z-index: 256; ' +
        'padding: 10px 15px; ' +
        'margin: 0 0 0 -60px; ' +
        'border-radius: 4px; ' +
        'position: fixed; ' +
        'top: 50%; ' +
        'left: 50%; ' +
        'width: 130px; ' +
        'text-align: center;';

    document.body.appendChild(m);
    setTimeout(function () {
        var d = 0.5;
        m.style.opacity = '0';
        setTimeout(function () {
            document.body.removeChild(m)
        }, d * 1000);
    }, duration);
}