为什么这个用户脚本在 Facebook 上不起作用?

发布于 2024-11-28 07:38:54 字数 293 浏览 1 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-12-05 07:38:54

其他答案指出它应该是(domain == Face),而这一个错误。

但是,这并不是阻止脚本按预期工作的原因。

主要问题是 Chrome 用户脚本无法使用目标页面中定义的 JS。您必须将代码注入到页面中,如下所示:

function functionToInject () {
    function myCode () {
        /*--- This is where you put everything you want to do that 
            requires use of the page's javascript.
        */
        var face = "facebook.com"
        var domain = document.domain
        if (domain == face)
        {
            Chat.openTab ("sam.sebastian1", "Seb");
        }
    }
    myCode ();
}

function addJS_Node (text, s_URL) {
    var scriptNode                      = document.createElement ('script');
    scriptNode.type                     = "text/javascript";
    if (text)  scriptNode.textContent   = text;
    if (s_URL) scriptNode.src           = s_URL;

    var targ    = document.getElementsByTagName('head')[0] 
                || document.body || document.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node ( '(' + functionToInject.toString() + ')()' );


这是基本答案。 但是,由于这是 Facebook,所以事情有点复杂。

  1. Facebook 加载了许多 iFrame,并且脚本将在其中许多上触发。
  2. Chat 对象不会立即加载。

为了绕过这些障碍,我们设置了一个计时器,在找到资源之前它不会尝试执行我们的代码。

就像这样:

function functionToInject () {
    function myCode () {
        /*--- This is where you put everything you want to do that 
            requires use of the page's javascript.
        */
        var face = "facebook.com"
        var domain = document.domain
        if (domain == face)
        {
            Chat.openTab ("sam.sebastian1", "Seb");
        }
    }

    var waitForKeyElements  = setInterval (checkForElement, 500);

    function checkForElement () {
        if (typeof Chat != "undefined" ) {
            clearInterval (waitForKeyElements);
            myCode ();
        }
    }
}

function addJS_Node (text, s_URL) {
    var scriptNode                      = document.createElement ('script');
    scriptNode.type                     = "text/javascript";
    if (text)  scriptNode.textContent   = text;
    if (s_URL) scriptNode.src           = s_URL;

    var targ    = document.getElementsByTagName('head')[0] 
                || document.body || document.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node ( '(' + functionToInject.toString() + ')()' );

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:

function functionToInject () {
    function myCode () {
        /*--- This is where you put everything you want to do that 
            requires use of the page's javascript.
        */
        var face = "facebook.com"
        var domain = document.domain
        if (domain == face)
        {
            Chat.openTab ("sam.sebastian1", "Seb");
        }
    }
    myCode ();
}

function addJS_Node (text, s_URL) {
    var scriptNode                      = document.createElement ('script');
    scriptNode.type                     = "text/javascript";
    if (text)  scriptNode.textContent   = text;
    if (s_URL) scriptNode.src           = s_URL;

    var targ    = document.getElementsByTagName('head')[0] 
                || document.body || document.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node ( '(' + functionToInject.toString() + ')()' );


That was the basic answer. However, since this is Facebook, things are a bit more complicated.

  1. Facebook loads many iFrames, and the script will trigger on many of them.
  2. The 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:

function functionToInject () {
    function myCode () {
        /*--- This is where you put everything you want to do that 
            requires use of the page's javascript.
        */
        var face = "facebook.com"
        var domain = document.domain
        if (domain == face)
        {
            Chat.openTab ("sam.sebastian1", "Seb");
        }
    }

    var waitForKeyElements  = setInterval (checkForElement, 500);

    function checkForElement () {
        if (typeof Chat != "undefined" ) {
            clearInterval (waitForKeyElements);
            myCode ();
        }
    }
}

function addJS_Node (text, s_URL) {
    var scriptNode                      = document.createElement ('script');
    scriptNode.type                     = "text/javascript";
    if (text)  scriptNode.textContent   = text;
    if (s_URL) scriptNode.src           = s_URL;

    var targ    = document.getElementsByTagName('head')[0] 
                || document.body || document.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node ( '(' + functionToInject.toString() + ')()' );
趴在窗边数星星i 2024-12-05 07:38:54
if (domain == face)
{
 Chat.openTab("sam.sebastian1", "Seb") 
}

不是

if (domain = face)
{
 Chat.openTab("sam.sebastian1", "Seb") 
}
if (domain == face)
{
 Chat.openTab("sam.sebastian1", "Seb") 
}

not

if (domain = face)
{
 Chat.openTab("sam.sebastian1", "Seb") 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文