我正在尝试将活动标签Url设置为我在其上注入的iframe中的表单字段chrome.browserAction.onClicked
。
问题是由于安全策略(主页和iframe的不同来源),我无法iframe
从访问content script
。主页是一个外部页面。例如。wikipedia.com
到目前为止,这是我尝试过的。
content_script.js
function onExtensionMessage(request) {
if (request['iconClicked'] != undefined) {
var extensionOrigin = 'chrome-extension://' + chrome.runtime.id;
if (!location.ancestorOrigins.contains(extensionOrigin)) {
var urlvalue = location.href;
var iframe = document.createElement('iframe');
iframe.setAttribute("id", "iFrameS");
iframe.src = chrome.runtime.getURL('popup.html');
iframe.addEventListener('load', function (e) {
// Either set the value of input element in Iframe
// here or pass an event to background.js and set it from there
chrome.extension.sendRequest({'frameloaded': true});
}, false);
document.body.appendChild(iframe);
}
return;
}
}
function initContentScript() {
chrome.extension.onRequest.addListener(onExtensionMessage);
}
initContentScript();
popup.html
<form method="post">
<input id="url" type="text">
</form>
单击扩展程序图标后,我将一条消息传递到内容脚本以注入iframe,一旦加载iframe,我想设置字段值。
我从内容脚本中引用了此链接Access iframe,但找不到解决方案。
任何帮助,将不胜感激。
如果需要将简单数据传递到新创建的iFrame,则可以在iFrame的src中使用查询字符串。
创建iFrame时:
var winLoc = window.location.href; //I think this is what you want to send to your iFrame and populate field value
iframe.src = chrome.runtime.getURL('popup.html?'+winLoc );
在您的iFrame脚本拆分网址中:
var activeUrl = location.href.split('?')[1];
//activeUrl now contains passed data
这样,您可以随意连接多个“简单”数据
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句