qJake
qJake

Reputation: 17139

Call JavaScript function on a page automatically with Chrome?

When I load a particular webpage, I'd like to call a Javascript function that exists within their page. I could use a bookmarklet with javascript:TheFunction();, but I'd like to know if there's an easier way to do it, either with a Chrome extension or otherwise.

Upvotes: 0

Views: 4555

Answers (3)

Sensatus
Sensatus

Reputation: 89

An alternative option is to modify the javascript function to make it globally accessible from the [Chrome] debug console.

Change the function from for example

function foo(data)

to

foo = function(data)

then using the debug console, call the method with the attributes required

data = {my: "data"}
foo(data)

Upvotes: -1

Johnny Craig
Johnny Craig

Reputation: 5000

Chrome extensions run in a sandbox so you cannot call a function directly from the webpage code how you want. you either have to use javascript:fuction(); and set document.location, or you can create script elements on the page with a callback to your own extension. check out how this guy did it:

https://convore.com/kynetx/kbx-writing-durable-code/

i am refering to this post, and the one above and below it specifically

var anewscript = document.createElement("script");
anewscript.type = 'text/javascript';
anewscript.innerHTML=' callback_tmp= function (mydata){ ' +
' document.getElementById("someElement").onclick(mydata);' +
'}';
document.getElementsByTagName("head")[0].appendChild(anewscript);

Upvotes: 1

Joseph Marikle
Joseph Marikle

Reputation: 78590

With chrome, you can either install a grease monkey script directly or get the Blank Canvas script handler plugin (the latter of which I use).

Upvotes: 3

Related Questions