0

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.

1

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Looks like this does most of the heavy lifting for me, thanks!
Glad to help :) it is really easy to use and a wonderful extension (I use it all the time).
1

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);

Comments

-1

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)

1 Comment

This has nothing to do with what I wanted, I didn't say anything about a console. I wanted to have some JavaScript code injected and executed when the page loaded in my extension. You're also over 1 year late to this party, and I already accepted the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.