原文: 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;
})();