genesis
genesis

Reputation: 50974

Simple user script isn't working anywhere?

I have this code

//
// ==UserScript==
// @name           test
// @description    test
// @version        0.2
// @namespace      test
// @include        *
// ==/UserScript==
$(function(){
    alert('d');
});

and when I'm trying to execut it in my chrome, it installs as an extension, but executes nowhere (stackoverflow has jQuery already so I think I don't need to include that again. )

What's wrong?

Upvotes: 2

Views: 135

Answers (1)

Brock Adams
Brock Adams

Reputation: 93523

Chrome userscript JS and the page's JS cannot interact with each other. You'll need to inject your JS into the page...

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] || d.body || d.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node ("alert('d');");

Upvotes: 3

Related Questions