Why doesn't this userscript work on Facebook?

后端 未结 2 1272
花落未央
花落未央 2020-12-21 13:18

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.open

相关标签:
2条回答
  • 2020-12-21 14:02

    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() + ')()' );
    
    0 讨论(0)
  • 2020-12-21 14:09
    if (domain == face)
    {
     Chat.openTab("sam.sebastian1", "Seb") 
    }
    

    not

    if (domain = face)
    {
     Chat.openTab("sam.sebastian1", "Seb") 
    }
    
    0 讨论(0)
提交回复
热议问题