Greasy Fork

Greasy Fork is available in English.

💡WebPreview - 信息直达

支持国内主流搜索引擎的搜索结果快速预览(直达网页文本大纲)

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         💡WebPreview - 信息直达
// @namespace    https://ez118.github.io/
// @version      0.2
// @description  支持国内主流搜索引擎的搜索结果快速预览(直达网页文本大纲)
// @author       ZZY_WISU
// @match        https://*.bing.com/*
// @match        https://www.baidu.com/*
// @match        https://www.so.com/*
// @connect      *
// @license      GNU GPLv3
// @icon         https://cn.bing.com/sa/simg/favicon-trans-bg-blue-mg.ico
// @run-at document-end
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @grant        GM_registerMenuCommand
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

var ReaderFlag1 = false; /* 用于存储阅读器元素是否被创建 */
var ReaderFlag2 = false; /* 用于存储阅读器是否为开启状态 */

function runAsync(url,send_type,data_ry) {
    var p = new Promise((resolve, reject)=> {
        GM_xmlhttpRequest({
            method: send_type, url: url, headers: {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"}, data: data_ry,
            onload: function(response){resolve(response.responseText);}, onerror: function(response){reject("请求失败");}
        });
    });
    return p;
}

function escapeHtml(str) {
    return str.replace(/[&<>"']/g, function(match) {
        switch (match) {
           case '&':
              //return '&amp;';
              return '&';
           case '<':
              return '&lt;';
           case '>':
              return '&gt;';
           case '"':
              return '&quot;';
           case '\'':
              return '&#39;';
           default:
              return '';
        }
    });
}

function getWebContents(txt) {
    var links;
    var images;
    var content;
    /* 获取所有链接 */
    /*links = txt.match(/href\=\"(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g);
    for(let i = 0; i < links.length; i ++) {
        links[i] = links[i].replace("href=\"", "");
    }*/
    links = [];
    txt.replace(/<a [^>]*href=['"]([^'"]+)[^>]*>/g,function(match, capture){
        links.push(capture);
    });
    //console.log(links);

    /* 获取所有图片 */
    //images = txt.match(/<img[^<>]*src=[\"]([^\"]+)[\"][^<>]*>/im);
    images = [];
    txt.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g,function(match, capture){
        images.push(capture);
    });
    //console.log(images);

    /* 获取文本 */
    content = txt.replace(/<\/div>/g, "</div>\n").replace(/<\/table>/g, "</table>\n")
    content = content.replace(/<\/h3>/g, "</h3>\n").replace(/<\/p>/g, "</p>\n")
    content = content.replace(/<\/li>/g, "</li>\n").replace(/<br>/g, "\n")
    content = content.replace(/<br\/>/g, "\n").replace(/<script(.*?)<\/script>/gis, "")
    content = content.replace(/<style(.*?)<\/style>/gis, "").replace(/<nav(.*?)<\/nav>/gis, "");

    content = content.replace(/<a(.*?)<\/a>/gis, "");

    content = content.replace(/<[^>]+>/g, "")
    content = escapeHtml(content);

    var content_len = content.length;
    content = content.substring(0, content_len);
    const search_txt = ["  ", "  ", "\n\n", "\r\r", "\t\t", "\n\r\n\r", "\r\n\r\n", "\t\r\n"];
    const replace_txt = ["", "", "", "", "", "", "", ""];
    for (let i = 0; i < search_txt.length; i++) {
        content = content.split(search_txt[i]).join(replace_txt[i]);
    }
    
    content = content.replace(/\r\n/g,"<br/>").replace(/\n/g,"<br/>").replace(/<br\/><br\/><br\/>/g, "");
    //console.log(content);

    return {"link":links,"image":images,"content":content};
}

function openReader(url) {
    if (ReaderFlag1 == false) {
        let previewReader = document.createElement("div");
        previewReader.setAttribute("class", "userscript-webPreviewReader");
        previewReader.setAttribute("style", "display:block;");
        previewReader.setAttribute("id", "userscript-webPreviewReader");
        document.body.appendChild(previewReader);
        ReaderFlag1 = true;
        ReaderFlag2 = true;
    } else {
        let previewReader = document.getElementById("userscript-webPreviewReader");
        previewReader.setAttribute("style", "display:block;");
        ReaderFlag2 = true;
    }
    var previewReader = document.getElementById("userscript-webPreviewReader");
    previewReader.innerHTML = "<b>加载中...</b>";

    runAsync(url, "GET", "").then((result)=>{ return result; }).then(function(result){
        let reslist = getWebContents(result);
        let linkhtml = "", imghtml = "";


        for(let i = 0; i < reslist.link.length; i ++){
            let link_tmp = reslist.link[i];
            if(link_tmp.includes("https://") || link_tmp.includes("http://")){
                linkhtml += "<a class='link' target='_blank' href='" + link_tmp + "'> 🔗&nbsp;" + link_tmp + " </a><br>";
            }
        }

        for(let i = 0; i < reslist.image.length; i ++){
            imghtml += "<img class='image' src='" + reslist.image[i] + "' onerror='this.remove()'/>";
        }

        previewReader.innerHTML = `
        <button class="CloseButton" onclick='this.parentNode.setAttribute("style", "display:none;");'>关闭</button>
        <div class="ImageList" style="max-height:103px;">
			<!--<b>图像: </b><br/>-->
            <p class='ShowImgList' align='right' style='' onclick='this.parentNode.setAttribute("style", "");'>所有图片</p>
            ` + imghtml + `
		</div>

		<div class="LinkList" style="max-height:286px;">
            <p class='ShowImgList' align='right' style='' onclick='this.parentNode.setAttribute("style", "");'>所有链接</p>
            ` + linkhtml + `
		</div>
		<div class="ContentShow">
			<b>文本: </b>
            ` + reslist.content + `
		</div>`;
    });
}

(function() {
    'use strict';
    var url = window.location.href;
    var paths = url.split("/");

    GM_addStyle(`.userscript-webPreviewBtn { background:#FFF; padding:3px 13px; margin-left:5px; border-radius:15px; border:1px solid #666; cursor:pointer; }
                 .userscript-webPreviewBtn:active { background:#eee; }
                 .userscript-webPreviewReader { position:fixed; top:0; right:0; z-index:9999; width:40%; height:100%; min-width:350px; background:#FFF; overflow:auto; }
                 .ShowImgList{ margin:0;padding:0;width:100%;cursor:pointer;color:#138AF1;user-select:none; }
                 .image{ height:85px; margin-bottom:8px; margin-right:5px; border-radius:15px; object-fit:contain; max-width:calc(100% - 20px); }
                 .link{ text-decoration:none; color:#001ba0; }
                 .link:hover{ text-decoration:underline; }
                 .ImageList{ padding:10px; margin:5px; border:2px solid #f4effb; background:#f4effb; border-radius:15px; overflow:hidden; }
                 .LinkList{ padding:10px; margin:5px; border:2px solid #f4f1f6; background:#f4f1f6; border-radius:15px; overflow:hidden; }
                 .ContentShow{ padding:10px; margin:5px; border:2px solid #f2f2f2; background:#f2f2f2; border-radius:15px; }
                 .CloseButton{ background:#FFF; color:#000; padding:3px 13px; margin:5px 5px; margin-bottom:0px; border-radius:15px; border:1px solid #666; cursor:pointer; }
                 .CloseButton:hover{ background:#138AF1; border:1px solid #138AF1; color:#FFF; }
                 `);

    if (paths[2].includes("bing.com") && paths[3].includes("search")) {
        let resultItems = document.getElementsByClassName("b_algo");
        for(let i = 0; i < resultItems.length; i ++) {
            let resultItemLink = resultItems[i].getElementsByTagName("a")[0].href;
            let resultItemTitleEle = resultItems[i].getElementsByTagName("h2")[0];

            /* 向每一个搜索结果的标题部分添加按钮 */
            let previewBtn = document.createElement("button");
            let previewBtnTxt = document.createTextNode("💡");
            previewBtn.setAttribute("class", "userscript-webPreviewBtn");
            previewBtn.setAttribute("link-data", resultItemLink);
            resultItemTitleEle.appendChild(previewBtn);
            previewBtn.appendChild(previewBtnTxt);

            previewBtn.addEventListener("click", function(evt){
                let linkData = previewBtn.getAttribute("link-data");
                openReader(linkData);
            }, true);
        }
    }
})();