WeChat Image Copier 微信公众号图片复制

点击微信公众号文章图片,立即复制到粘贴板

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         WeChat Image Copier 微信公众号图片复制
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  点击微信公众号文章图片,立即复制到粘贴板
// @author       maquedexiju
// @match        https://mp.weixin.qq.com/*
// @grant        none
// @run-at       document-idle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function add_copy_img_event() {
        console.log('run');

        var imgs = document.querySelectorAll('img')

        function add_event(img) {

            img.addEventListener('click', function(event) {
                // 找到图片元素
                var imgElement = event.currentTarget;
                console.log(imgElement.src);
                imgElement.setAttribute('crossorigin', 'anonymous'); // 解决 Tainted canvases may not be exported.
                var canvas = document.createElement('canvas');

                // 将图片绘制到canvas上
                canvas.width = imgElement.width;
                canvas.height = imgElement.height;
                var ctx = canvas.getContext('2d');
                ctx.drawImage(imgElement, 0, 0, imgElement.width, imgElement.height);
                // document.body.appendChild(canvas); // 测试图片有没有正常获取

                // 转为Blob数据
                canvas.toBlob(blob => {
                    // 使用剪切板API进行复制
                    const data = [new ClipboardItem({
                        ['image/png']: blob
                    })];

                    navigator.clipboard.write(data).then(function(){console.log('成功')}, function(){console.log('失败')});
                });

            })
        };


        imgs.forEach(add_event);
    };

    // setTimeout(add_copy_img_event, 1000); // 10000毫秒 = 10秒
    var element = document.body;
    // 添加滚动事件的监听器
    window.addEventListener('scrollend', add_copy_img_event); // 微信的内容是划到了地方才进行内容加载
    })();