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

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

当前为 2024-09-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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); // 微信的内容是划到了地方才进行内容加载
    })();