Greasy Fork is available in English.

Discussions » Greasy Fork Feedback

关于复制网页内容和标题、地址URL为markdown格式,找不到脚本的作者了,需要请教复制网页内容丢失问题

§
Posted: 22.10.2022
Edited: 22.10.2022

因为把这个脚本修改了,去掉自动更新,还修改了脚本内容,找不到脚本主页了。 请帮忙找一下脚本主页(修改后的脚本内容如下,只知道脚本作者@author:zhanyaha,使用用户搜索没找到人),另外发现这个脚本对有些网页的复制内容,处理后没有了,只有标题和地址URL了? 请知道的帮忙提供一下线索或问题解决思路。

// @copyright 2019, zhanyaha
// @license MIT
// @author       zhanyaha
// @match        *://*/*
// @require      https://unpkg.com/turndown/dist/turndown.js
// @require      https://unpkg.com/turndown-plugin-gfm/dist/turndown-plugin-gfm.js
// @grant        GM_setClipboard
(function() {
    'use strict';
    function commonParentNode(oNode1, oNode2) {
        if(oNode1===oNode2){
            return oNode1.parentNode;
        }
        for(;;oNode1=oNode1.parentNode) {
            if(oNode1.contains(oNode2)){
                return oNode1;
            }
        }
    }
    //复制md的格式:鼠标选取内容 + From + 文章标题及链接 + 日期 时间
    document.addEventListener("keydown", function(e) {
        if(e.ctrlKey && e.keyCode == 90 ){
            var sel=document.getSelection();
            var relative_pos=sel.anchorNode.compareDocumentPosition(sel.focusNode);
            var content = commonParentNode(sel.anchorNode,sel.focusNode);
            let fragment = document.createDocumentFragment();
            if(content.children.length===0){
                fragment.appendChild(content.cloneNode(true));
            }
            for(let item of content.children){
                var p1 = item.compareDocumentPosition(sel.anchorNode);
                var p2 = item.compareDocumentPosition(sel.focusNode);
                if(relative_pos!==2){
                    if(p1===4 || p2===2){
                        continue;
                    }
                }else{
                    if(p1===2 || p2===4){
                        continue;
                    }
                }
                fragment.appendChild(item.cloneNode(true));
            }
            var imgDatas = '';
            var count = 0;
            function getMarkdown(container){
                var turndownService = new TurndownService({ codeBlockStyle: 'fenced' });
                var gfm = turndownPluginGfm.gfm;
                turndownService.use(gfm)
                turndownService.keep(['pre']);
                turndownService.remove('script')
                turndownService.addRule('image', {
                    filter: 'img',
                    replacement: function (content, node) {
                        return '!['+ node.alt+ '][' + node.id + ']';
                    }
                })
                var markdown = turndownService.turndown(container);
                let date = new Date();
                let timeStamp = ' [['+ date.toLocaleDateString("zh",{year: "numeric", month: "2-digit", day: "2-digit"}).replaceAll('\/', '-') + ']] ' + date.toLocaleTimeString('chinese', { hour12: false });
                //markdown = 'From: ['+document.title+']('+document.URL+') '+timeStamp+'\n\n'+markdown+imgDatas;  //without From
                markdown = ' ' + markdown + imgDatas + ' \n\n \t\t From: [' + document.title+ '](' + document.URL+') ' + timeStamp;
                GM_setClipboard(markdown);
            }
            var imgs = fragment.querySelectorAll('img');
            if(imgs.length){
                imgs.forEach(function(item,index){
                    GM_xmlhttpRequest({
                        method: 'GET',
                        url:item.src,
                        responseType:'blob',
                        headers: {
                            referer:location.href
                        },
                        onload: function(res) {
                            let reader = new FileReader();
                            reader.onloadend = (function (i){
                                return function(){
                                    imgDatas += '\n\n[img'+i+']:'+reader.result;
                                    count++;
                                    if(count === imgs.length){
                                        getMarkdown(fragment);
                                    }
                                };
                            })(index);
                            let blob = res.response.slice(0,res.response.size,/(?:content-type:\s*)(.*)/g.exec(res.responseHeaders)[1]);
                            reader.readAsDataURL(blob);
                            item.id = 'img'+index;
                        },
                        onerror: function(e) {
                            console.error(e);
                        }
                    });
                });
            }else{
                getMarkdown(fragment);
            }
        }
    })

Post reply

Sign in to post a reply.