gpt4 book ai didi

javascript - 如何在 chrome 扩展中匹配 youtube 的 url?

转载 作者:行者123 更新时间:2023-11-29 15:43:45 26 4
gpt4 key购买 nike

我正在尝试使简单的 chrome 扩展在 youtube 中工作(内容脚本),但它似乎无法正常工作。

奇怪的是它有时有效,而有些则无效。

我的最终目标是让它只在视频 URL 中工作,但在那之前我就卡住了。

我最初尝试过:

"matches": [ "http://www.youtube.com/*" ]

完全没有成功,我觉得这很奇怪,所以我想这是我的第一个问题,因为在我做的另一个扩展中,它非常适合 http://twitter.com/

我阅读了一些 youtube 扩展的代码,其中一个使用了:

"matches": [ "*://*.youtube.com/*" ]

奇怪的是,这只在某些时候有效,我无法重现/理解什么时候。当我重新加载页面时它肯定不起作用,但在某些 URL 中它可以正常工作。

实际脚本是用于调试的基本脚本:

$(window).load(function() {
alert(2);
});

所以我对这种不一致的行为感到很困惑。

编辑:

这个 url 的注意事项似乎有效:

这样做:

编辑 2:

整个 list :

{
"name": "__MSG_extname__",
"version": "0.1",
"default_locale": "en",
"manifest_version": 2,
"icons": {
"16": "images/icon16.png",
"48": "images/icon48.png",
"128": "images/icon128.png" },
"content_scripts":[
{
"matches": [ "*://*.youtube.com/*" ],
"js": ["jquery.min.js", "script.js"]
}
],
"web_accessible_resources": ["images/down-arrow.png"]
}

最佳答案

您需要张贴或链接到您的整个 manifest.json , 但最有可能的问题是 $(window).load( ... .
By default, Chrome content scripts fire at unpredictable times which could be either before or after window.onload fires!

因此,添加 "run_at": "document_end"到您的 list 以保证您的脚本将在可预测的时间运行,并在 window.onload 之前运行.

~~~
接下来,it is possible that other extensions are blocking that alert .根据需要检查并禁用您的其他扩展程序。

~~~
最后,可能是 YouTube 通过 AJAX 加载了一些页面——尽管我目前找不到它这样做的案例。加载 AJAX 的页面不会重新触发您的内容脚本。如果您发现 AJAX 加载"new"页面的情况,(1) 在此处评论食谱,(2) 您可以 use AJAX-aware techniques to refire code as necessary . (注意 waitForKeyElements.js 将通过 list 而不是 @require 指令添加。)

关于javascript - 如何在 chrome 扩展中匹配 youtube 的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14946632/

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