2

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?

genesis
  • 50,477
  • 20
  • 96
  • 125
  • Stack overflow uses `jQuery.noConflict()` so you'll need to use `jQuery` instead of `$`, but @Brock_Adams is right, you need to inject.. – Greg Guida Jul 21 '11 at 21:06
  • See: http://stackoverflow.com/questions/5006460/userscripts-greasemonkey-calling-a-websites-javascript-functions/5006952#5006952 – Wayne Jul 21 '11 at 21:27

1 Answers1

3

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');");
Brock Adams
  • 90,639
  • 22
  • 233
  • 295