Javascript 是否可以从扩展名调用父网页的iframe中的函数
我在网页上加载了iframe。是否可以从扩展名调用父网页的iframe中的函数 已完成所有帧:manifest.json中为trueJavascript 是否可以从扩展名调用父网页的iframe中的函数,javascript,jquery,html,google-chrome,iframe,Javascript,Jquery,Html,Google Chrome,Iframe,我在网页上加载了iframe。是否可以从扩展名调用父网页的iframe中的函数 已完成所有帧:manifest.json中为true iframe.html如下所示: <body> <div class="row"> <button type="button" class="btn btn-primary" id="manual-dial" onclick="dial()";>DIAL<
iframe.html
如下所示:
<body>
<div class="row">
<button type="button" class="btn btn-primary" id="manual-dial" onclick="dial()";>DIAL</button>
</div>
</body>
/* cnt access frame id */
chrome.runtime.sendMessage(window.parent.frames[1].document.id);
window.parent.frames[1].document.id.onload = function() {
// want to call dial() of iframe
chrome.runtime.sendMessage("this is my response");
}
background.js
chrome.runtime.onMessage.addListener(function(response,sender,sendResponse) {
alert(response);
});
您可以尝试以下方法:
iframe.html
<body>
<div class="row">
<button type="button" class="btn btn-primary" id="manual-dial" onclick="dial()";>DIAL</button>
</div>
</body>
<script type="text/javascript">
function dial(){
var portName = "dialPort"
var port = chrome.runtime.connect({name: portName});
//this postMessage is just to stimulate the listener in the background.js
port.postMessage({any: "anyThing"});
// You can recive the reponse
port.onMessage.addListener(function(msg) {
/** Here you can exec any function **/
});
</script>
您还可以查看Chrome文档,在这里您可以找到更多的实现
我希望这有助于复制
chrome.runtime.onConnect.addListener(function(port) {
if(port.name == "dialPort"){
port.onMessage.addListener(function(msg) {
/**
Here you can exec any function
**/
//And also, you can send the response to the iframe
port.postMessage({response: "Any Thing..."});
});
}});