简体   繁体   English

从内容脚本访问网页脚本

[英]Accessing webpage script from content script

I'm trying to execute the onclick() event of a link ( <a> ), but it does not take the visitor anywhere, it executes JavaScript code. 我正在尝试执行链接( <a> )的onclick()事件,但是它不会将访问者带到任何地方,它会执行JavaScript代码。 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): 怎么样(使用jQuery):

suppose the script-in-page contains the function 'myfunc' that you want to be able to execute. 假设页面中的脚本包含您希望能够执行的功能“ myfunc”。

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 : Edit2:您也可以再次“插入”代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM