link:nth--6ren">
gpt4 book ai didi

javascript - 我如何使用 tampermonkey href 本地文件?

转载 作者:行者123 更新时间:2023-11-28 15:06:16 24 4
gpt4 key购买 nike

我想通过 href 替换站点的 CSS 文件 URL,我知道可以引用包含 css 的外部链接,但是如何代替本地文件呢?

document.querySelector("head > link:nth-child(7)").href = "http://example.com/style.css"

最佳答案

一个选项是本地文件是一个脚本,也许是一个将所需的 CSS 文本分配给 window 的脚本。属性,然后可以在您的用户脚本中检索。例如:

// ==UserScript==
// @name local
// @match https://example.com
// @require file:///C:/local.js
// ==/UserScript==

document.head.appendChild(document.createElement('style'))
.textContent = window.cssTextFromLocal;

// local.js
window.cssTextFromLocal = `
body {
background-color: green;
}
`;

enter image description here

确保permit local file access .

当然,如果您想无条件地执行此操作(无论如何在每次页面加载时),就不需要脚本间通信,您可以插入 <style>进入DOM里面local.js .

关于javascript - 我如何使用 tampermonkey href 本地文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56943318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com