支持document-start
为@run-at
。
有关更多详细信息,请参阅#2483。 这可能是“不可能的”。
您可以使用content_scripts
清单键的run_at
属性。
4 年前,我与您分享了对 Chrome 的遗憾。 现在又是 Firefox。 😭
我想在https://bugs.chromium.org/p/chromium/issues/detail?id=257956 中重新表述我的建议
tabs.insertCSS/executeScript 应该插入 CSS/JS 直到选项卡的 URL 设置为非空(非空,非未定义,非空字符串“”),如果选项卡被删除/更改,则被取消
应该改进 WebExtension API。
@iology你为什么
以下应该解释如何使用我的方法这样做:(应该可靠地工作)
/**
* <strong i="8">@typedef</strong> UserScript
* <strong i="9">@property</strong> {Object} header The parsed header of a script
* <strong i="10">@property</strong> {String} script The raw script data
* <strong i="11">@property</strong> {String} type The script media type (in case a script is WebAssembly
* or something other than JavaScript)
*/
/** The load state of this script. Used by the `matches()` function */
const READY_STATE = document.readyState;
(async () => {
(await (browser.storage.local.get({scripts: []}) // Excuse this horrible code style
.then(ret => ret.scripts))) // of combining `await` and `.then()`
.forEach(/* <strong i="12">@param</strong> {UserScript} script */ script => {
if (matches(script.header)) {
let scriptTag = document.createElement("script");
scriptTag.setAttribute("type", script.type)
scriptTag.textContent = script.script;
document.appendChild(scriptTag);
}
});
})();
/**
* Checks if this script matches this page and @run-at property.
*
* <strong i="13">@param</strong> {Object} scriptHeader The parsed header of a script
* <strong i="14">@return</strong> {boolean} `true` if this page matches, `false` otherwise
*/
function matches(scriptHeader) {
// Uses window.location and READY_STATE.
// TODO: Implement
}
@ExE-Boss 当您从扩展存储中检索脚本时,页面的一部分已经被加载。
我知道这一点,但我并没有真正看到另一种方法,因为 WebExtensions 本质上是异步的。
@ExE-Boss,有一个 bugzilla, https: //bugzilla.mozilla.org/show_bug.cgi?id=1332273,看起来进展顺利,应该有助于解决 @document-start 问题。 它是否会登陆FF57,我不确定。
我知道这一点,但我并没有真正看到另一种方法,因为 WebExtensions 本质上是异步的。
@ExE-Boss 这就是我们要解决的问题。
我也在尝试解决它,但我的解决方案实际上只适用于 #2525,因为此时整个页面已完全加载。
Violentmonkey 也有这个问题,不确定 Tampermonkey 使用什么黑魔法,但它能够在真实的document-start
上可靠地注入脚本。
Tampermonkey 2.9 及更早版本是 GPL-3.0 并发布在 GitHub 上。 我试过调查它,起初我认为它正在使用sync XHR ,但在调查了一下之后,我不知道它是如何实现的,但它以某种方式做到了这一点,因为在 2.6 版中.2767 .
我对盗版不感兴趣,扭转 Tampermonkey 的逻辑不会太难,但这样做是不合法的。
@Sxderp这仅适用于 Firefox 57,该错误可能仍会在 58+ 中得到解决。
仅供参考,Tampermonkey 在文档开始时也不可靠。 有一个“即时”注入选项(非常hacky)可以加快速度,但是在像xkcd.com 这样的简单页面上,我仍然会在我的CSS(总是在文档开始时注入)加载之前得到一个闪光。 我也偶尔会在其他网站上得到闪光。
反转 Tampermonkey 的代码并不是解决这个问题的方法。 另外,他们使用的实际方法可以在其他用户建议的论坛中找到。 复制方法不是复制 vio,只是复制代码本身。
但我们知道这是一个不完美的解决方案,我们想要一个完美的解决方案。
如果找到一种方法来完成这项工作,是否有办法让脚本同步读取其配置选项的至少一个子集? 作为这将解决的问题类型的示例,请参阅https://github.com/ccd0/4chan-x/issues/1627。 在这里我需要禁用页面上的一些代码,但我也希望有一个不禁用代码的选项。 许多用户在每次浏览器会话结束时清除localStorage
。
如果找到一种方法来完成这项工作,是否有办法让脚本同步读取其配置选项的至少一个子集?
短期内不会。 无法以同步方式与 (get|set)Value 存储进行通信。 也许 GM 进行了某种注入,其中具有配置值的变量被注入到脚本的全局范围内。 这是我能想到的最好的,而且不是很好。
也许 GM 进行了某种注入,其中具有配置值的变量被注入到脚本的全局范围内。 这是我能想到的最好的,而且不是很好。
大概它是GM
对象的一部分。 类似GM.initValues.yourVariable
。