为什么这个用户脚本在 Facebook 上不起作用?
我正在为 Google Chrome 编写用户脚本,它会自动打开特定的聊天选项卡,但它不起作用,
我认为这是因为 Chat.openTab
没有明确定义,因为当我运行代码时在 javascript 控制台中它工作正常。
代码:
var face = "facebook.com"
var domain = document.domain
if (domain = face)
{
Chat.openTab("sam.sebastian1", "Seb")
}
I was writing user script for Google Chrome that would automatically open a specific chat tab, but it's not working,
I think that it is because the Chat.openTab
is not specifically defined, because when i run the code in the javascript console it works fine.
CODE:
var face = "facebook.com"
var domain = document.domain
if (domain = face)
{
Chat.openTab("sam.sebastian1", "Seb")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其他答案指出它应该是
(domain == Face)
,而这是一个错误。但是,这并不是阻止脚本按预期工作的原因。
主要问题是 Chrome 用户脚本无法使用目标页面中定义的 JS。您必须将代码注入到页面中,如下所示:
这是基本答案。 但是,由于这是 Facebook,所以事情有点复杂。
Chat
对象不会立即加载。为了绕过这些障碍,我们设置了一个计时器,在找到资源之前它不会尝试执行我们的代码。
就像这样:
The other answers point out that it should be
(domain == face)
, and this is an error.However, it is not what prevented the script from appearing to work as you expected.
The main problem is that Chrome userscripts cannot use JS defined in the target page. You must inject your code into the page, like so:
That was the basic answer. However, since this is Facebook, things are a bit more complicated.
Chat
object does not load right away.To get around these obstacles, we setup a timer, that doesn't try to execute our code until the resource is found.
Like so:
不是
not