Greasy Fork

zherop小工具

一键无图、隐藏图片、查看源代码

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

// ==UserScript==
// @name         zherop小工具
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  一键无图、隐藏图片、查看源代码
// @author       [email protected]
// @match        *://*/*
// @connect  static.61read.com
// @require      https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js
// @grant unsafeWindow
// @grant GM_xmlhttpRequest
// @grant GM_download
// @grant GM_openInTab
// @license      AGPL License
// ==/UserScript==

(function() {
    'use strict';
    // 定义
    var menus = [
        {"code":"noPic","name":"一键无图","event":"noPic"},
        {"code":"hidePic","name":"隐藏图片","event":"hidePic"},
        {"code":"viewSource","name":"查看源代码","event":"viewSource"},
        {"code":"downloadPng","name":"下载图片","event":"downloadPng"},
        {"code":"openIframe","name":"访问iframe","event":"openIframe"}
    ]

    // 菜单样式
    function menuStyle() {
        return `
            <style>
            .zp-menu {
                left: 0px;
                display: block;
                top: 50%;
                position: fixed;
                z-index: 9999;
                background: green;
                color: white;
                text-align: center;
                border-radius: 0 7px 7px 0px;
                font-size: 13px;
            }
            .zp-menu-item {
                padding: 5px;
                border: 1px solid;
                cursor: pointer;
            }

            @media screen and (max-width:768px) {
                .zp-menu {
                    font-size: 3.5vw
                }

            }

            </style>
         `
        }

    // 创建菜单DOM
    function createMenuDom() {
        var html = '<div id="zp-menu" class="zp-menu">'
        menus.forEach(item => {
            html += `<div class="zp-menu-item" type="${item.code}">${item.name}</div>`
        })
        html += '</div>'
        return html;
    }

    //  菜单事件
    // 一键无图
    function noPic(){
        $("img").remove();
        $("svg").remove();
    }

    // 隐藏图片
    function hidePic(){
        $("img").hide();
        $("svg").hide();
    }

    // 查看源代码
    function viewSource() {
        GM_openInTab('view-source:' + window.location.href)
    }

    // 下载图片
    function downloadPng() {
        var currentHref = window.location.href
        if(currentHref.startsWith('http://static.61read.com/')) {
            var baseURL = currentHref.substr(0,currentHref.lastIndexOf('/'))
            var xmlFile = unsafeWindow.xmlFile
            var pageFile = unsafeWindow.pageFile
            GM_xmlhttpRequest({
                method: "GET",
                url: baseURL + '/'+ pageFile + xmlFile,
                headers: {
                    "Content-Type": "application/xml"
                },
                onload: function(response) {
                    var responseXML = response.responseXML
                    var items = responseXML.getElementsByTagName('item')
                    for(var i=0;i<items.length;i++){
                        var path = items[i].getAttribute('href')
                        var imageURL = baseURL + '/'+ pageFile + path
                        console.log(imageURL)
                        var name = i + '.png'
                        if(path.lastIndexOf('/') > 0) {
                            name = path.substring(path.lastIndexOf('/') + 1)
                        }
                        download(imageURL,name);
                    }
                }
            });
        } else {
        }
    }

    function download(filePath,name){
        fetch(filePath).then(res => res.blob()).then(blob => {
            const a = document.createElement('a');
            document.body.appendChild(a)
            a.style.display = 'none'
            // 使用获取到的blob对象创建的url
            const url = window.URL.createObjectURL(blob);
            a.href = url;
            // 指定下载的文件名
            a.download = name;
            a.click();
            document.body.removeChild(a)
            // 移除blob对象的url
            window.URL.revokeObjectURL(url);
        });
    }

    function download2(url,name) {
        GM_download({
            url: url,
            onerror: function (error) {
                console.log(error)
            },
            onprogress: (pro) => {
                console.log(pro.loaded) //文件加载量
                console.log(pro.totalSize) //文件总大小
            },
            ontimeout: () => {
                //如果此下载由于超时而失败,则要执行的回调
            },
            onload: () => {
                //如果此下载完成,则要执行的回调
            }
        })
    }

    // 打开iframe
    function openIframe() {
        if(document.getElementsByTagName("iframe").length > 0){
            window.open(document.getElementsByTagName("iframe")[0].src)
        } else {
            console.log('不存在iframe')
        }
    }

    function copyText(text) {
        if($('#zp-copy').length <= 0) {
            $("body").append('<textarea id="zp-copy" style="display:none;"></textarea>')
        }
        $('#zp-copy').text(text).show();
        var ele = document.getElementById("zp-copy");
        ele.select();
        document.execCommand('copy', false, null);
        $('#zp-copy').hide();
        alert('复制链接成功!');
    }

    $(function(){
        $("head").append(menuStyle())
        $("body").append(createMenuDom())

        if(document.getElementsByTagName("iframe").length == 0){
            $(".zp-menu-item[type='openIframe']").hide()
        }


        // 菜单绑定事件
        $("#zp-menu .zp-menu-item").click(function(){
            var opType = $(this).attr("type")
            var currentMenu = menus.find(item => item.code===opType)
            if(currentMenu) {
                eval(currentMenu.event + "()")
            }
        })
    })
})();