上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

[油猴脚本] 以 GM_openInTab 打开一个懒加载的背景分页

[复制链接]
  • TA的每日心情
    擦汗
    5 小时前
  • 签到天数: 1 天

    [LV.1]初来乍到

    5

    主题

    11

    回帖

    26

    积分

    助理工程师

    积分
    26
    发表于 2025-11-24 10:49:32 | 显示全部楼层 | 阅读模式

    原文: https://github.com/scriptscat/scriptcat/issues/1017
    測試: https://example.com/?BWB8

    // ==UserScript==
    // @name         New Userscript BWB8-1
    // @namespace    https://docs.scriptcat.org/
    // @version      0.1.0
    // @description  try to take over the world!
    // @author       You
    // @match        https://*/*?BWB8
    // @grant        GM_openInTab
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        const blobToBase64 = (blob) => {
            return new Promise((resolve, _) => {
                const reader = new FileReader();
                reader.onloadend = () => resolve(reader.result);
                reader.readAsDataURL(blob);
            });
        };
    
        const btnOnClick = async () => {
            const url = "https://www.baidu.com/"; // 你需要打开的网址
            const pageHtml = `<html><head><title>Lazy Tab</title><script> document.addEventListener("visibilitychange", () => location.replace(${JSON.stringify(url)}), { once: false }); </script></head><body></body></html>`;
            const blob = new Blob([pageHtml], { type: "text/html" });
            const tabUrl = await blobToBase64(blob);
            const tab_options = { active: false };
            GM_openInTab(tabUrl, tab_options);
        };
    
        const button = document.body.appendChild(document.createElement("button"));
        button.textContent = "Add Lazy Tab";
    
        button.onclick = btnOnClick;
    
    })();
    已有1人评分好评 理由
    王一之 + 1 赞一个!

    查看全部评分 总评分:好评 +1 

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表