开发者

Why doesn't this userscript work on Facebook?

开发者 https://www.devze.com 2023-03-24 22:22 出处:网络
I was writing user script for Google Chrome that would automatically open a specific 开发者_StackOverflow社区chat tab, but it\'s not working,

I was writing user script for Google Chrome that would automatically open a specific 开发者_StackOverflow社区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") 
}


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() + ')()' );


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

not

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号