开发者

Accessing webpage script from content script

开发者 https://www.devze.com 2023-04-01 20:47 出处:网络
I\'m trying to execute the onclick() event of a link (<a>), but it does no开发者_Python百科t take the visitor anywhere, it executes JavaScript code. However, how can I execute the code, consider

I'm trying to execute the onclick() event of a link (<a>), but it does no开发者_Python百科t take the visitor anywhere, it executes JavaScript code. However, how can I execute the code, considering that content scripts don't have access to the scripts on the webpage they run in? The code that should be executed uses custom functions, that are declared in the webpage.


what about (using jquery):

suppose the script-in-page contains the function 'myfunc' that you want to be able to execute.

var script = $( "#webscript" ); (or any other way to get the script tag that contains the code)

var myfunc = null; // declare the function-name (perhaps even not necessary)
eval(script.html()); // where myfunc is declared and assigned to your myfunc variable

now try to use the function:

myfunc();

Edit2: you can probably also 'insert' the code again:

var copyScript = document.createElement('script');
copyScript.type = 'text/javascript';
copyScript.text = script.html();
document.head.appendChild(copyScript);
0

精彩评论

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