简体   繁体   English

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

[英]Why doesn't this userscript work on Facebook?

I was writing user script for Google Chrome that would automatically open a specific chat tab, but it's not working, 我正在为Google Chrome编写用户脚本,该脚本会自动打开特定的聊天标签,但无法正常工作,

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. 我认为这是因为Chat.openTab没有特别定义,因为当我在javascript控制台中运行代码时,它工作正常。

CODE: 码:

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

应该

if (domain === face)

The other answers point out that it should be (domain == face) , and this is an error. 其他答案指出它应该是(domain == face) ,这一个错误。

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. 主要问题是Chrome用户脚本无法使用目标页面中定义的JS。 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. 但是 ,由于这是Facebook,所以情况有点复杂。

  1. Facebook loads many iFrames, and the script will trigger on many of them. Facebook加载了许多iFrame,脚本将在其中许多触发器上触发。
  2. The Chat object does not load right away. Chat对象不会立即加载。

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() + ')()' );
if (domain == face)
{
 Chat.openTab("sam.sebastian1", "Seb") 
}

not

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM