Greasy Fork

Greasy Fork is available in English.

微信读书图片复制和下载

为了方便在微信读书看技术书复制图片用的,点击按钮新标签页打开图片的功能,还有图片下载的功能,图片下载的名称是按照时间戳为图片名的。

目前为 2024-03-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         微信读书图片复制和下载
// @namespace    http://tampermonkey.net/
// @version      0.2.5
// @description  为了方便在微信读书看技术书复制图片用的,点击按钮新标签页打开图片的功能,还有图片下载的功能,图片下载的名称是按照时间戳为图片名的。
// @author       You
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @match        https://weread.qq.com/web/reader*
// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_openInTab
// @grant        GM_download
// @grant        GM_setClipboard
// @grant        GM_notification
 
 
// ==/UserScript==
 
(async function() {
    'use strict';
 
    // 基于jQuery检测dom出现
    function jianceDOM(classname){
        return new Promise(res=>{
            let max=80;
            let jiance=setInterval(()=>{
                if(document.querySelectorAll(classname).length){
                    clearInterval(jiance)
                    res(true)
                }
                if(max<=0){
                    clearInterval(jiance)
                    res(false)
                }
                max--
            },100)
        })
    }
 
    // 检测文章内容发生变化
    // $(document).on("DOMNodeInserted","pre", async()=>{
    //     console.log('文章发生变化了')
    //     add_copy_code_btn()
    //     add_copy_img_btn()
    // })
    $("body").append(`
    <div id="module_box" style="
    position: fixed;
    left:0;
    top:200px;
    bottom:0;
    right:0;
    margin:auto;
    width: 200px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    background-color: rgba(0, 0, 0, 0.3);
    font-size: 24px;
    z-index:999999;
    display:none;">复制成功</div>
    `)
 

 
    $(document).on("click","button[title='下一章']",function(){
        add_copy_img_btn()
    })
    $(document).on("click",".chapterItem",function(){
        add_copy_img_btn()
    })
 
    async function add_copy_img_btn() {
        let res_dom_img = await jianceDOM('.wr_readerImage_opacity')
        if (res_dom_img) {
            $('.wr_readerImage_opacity').each((ind,ele) => {
                let btn =  document.createElement("button")
                btn.name = "btn_cxy_open_img_page"
                btn.innerHTML = "📋"
 
                let btn2 =  document.createElement("button")
                btn2.name = "btn_cxy_get_img"
                btn2.innerHTML = "▼"
 
                // 设置指定位置
                let rect = ele.getBoundingClientRect()
 
                btn.style.cssText = `position: absolute;right: 0px;top: ${rect.top - 50}px;color:white;z-index:9999; cursor:pointer`;
                btn2.style.cssText = `position: absolute;right: 0px;top: ${rect.top - 20}px;color:#888;z-index:9999; cursor:pointer`;

                // 将按钮添加到body中,而不是紧跟在元素后
                document.body.appendChild(btn);
                document.body.appendChild(btn2);
            })
        }
    }
    add_copy_img_btn()
 
 
    // 打开新窗口 显示图片
    $(document).on("click","button[name='btn_cxy_open_img_page']",function(){
        let link = $(this).prev().prev().attr("src")
        GM_openInTab(link, { active: true });
    })
 
    // 下来图片按钮
    $(document).on("click","button[name='btn_cxy_get_img']",function(){
        let link = $(this).prev().attr("src")
        // console.log(link);
        GM_download({
            url: link,
            name: new Date().getTime()+'.jpg',
            headers: {
              "User-Agent": "netdisk;6.7.1.9;PC;PC-Windows;10.0.17763;WindowsBaiduYunGuanJia",
            },
            onprogress: function (e) {
            //   console.log(JSON.stringify(e))
            },
        });
    })
})();