crossrider sidepanel只是一个iframe (您可以使用js注入的html,但我感兴趣的是使用iframe来减少对页面其余部分的干扰)。我很难在浏览器扩展和iframe之间进行任何交互。
我认为添加带有扩展的sidepanel毫无意义,除非您可以进行一些基本的JS通信。在这种情况下,我想要一些选项,复选框等,在iframe控制扩展。既然这个插件存在,我想肯定会有办法的。
理想情况下,我希望在子iframe中有一些基本的输入处理js,并让它返回奇怪的保存/加载命令。答案真的是某种形式的信息传递吗?如果是这样的话,我应该在这里使用哪个API?
我相信这是相关的:从chrome扩展访问iframe
编辑
好吧,我试过几件事.
html侧栏创建一个iframe,并在使用myiframe.contentWindow.document.open/writeln/close()延迟100 ID之后注入内容。这在chrome上工作得很好,但是在firefox中失败了,有一个安全错误(The operation is insecure on open())。src url提供iframe内容(对于侧栏,我使用url属性的数据地址):Html代码作为IFRAME源而不是URL。这适用于火狐,但在chrome:The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "data". Protocols must match.和Warning: Blocked a frame with origin "http://localhost" from accessing a cross-origin frame. Function-name: appAPI.message.addListener中会导致CORS错误。这些CORS的问题让我觉得非常愚蠢。所有的代码都来自同一个扩展,注入到同一个页面中。根本没有交叉起源,我创造了这该死的东西。如果我有能力改变原点,那么它从一开始就不安全,所以为什么要麻烦呢?
发布于 2013-12-15 11:28:32
假设您使用url侧栏属性来加载侧边栏的(即托管的网页),您可以在Iframe特性中使用扩展的运行来在iframe扩展和父窗口的扩展之间通信。
为此,首先启用扩展在iframes中运行(Settings > Run ),然后可以使用extension.js加载侧边栏并处理消息传递。例如,以下代码加载一个带有标识符btnSave的按钮的页面
托管网页文件:
<html>
<head>
</head>
<body>
<div id="mySidebar">
My sidebar form
<br />
<button id="btnSave">Save</button>
</div>
</body>
</html>extension.js文件:
appAPI.ready(function($) {
// Check if running in iframe and the sidebar page loaded
if (appAPI.dom.isIframe() && $('#mySidebar').length) {
// Set click handler for button to send message to parent window
$('#btnSave').click(function() {
appAPI.message.toCurrentTabWindow({
type:'save',
data:'My save data'
});
});
// End of Iframe code ... exit
return;
}
// Parent window message listener
appAPI.message.addListener(function(msg) {
if (msg.type === 'save') {
console.log('Extn:: Parent received data: ' +
appAPI.JSON.stringify(msg.data));
}
});
// Create the sidebar
var sidebar = new appAPI.sidebar({
position:'right',
url: 'http://yourdomain.com/sidebar_page.html',
title:{
content:'Sidebar Title',
close:true
},
opacity:1.0,
width:'300px',
height:'650px',
preloader:true,
sticky:true,
slide:150,
openAction:['click', 'mouseover'],
closeAction:'click',
theme:'default',
scrollbars:false,
openOnInstall:true,
events:{
onShow:function () {
console.log("Extn:: Show sidebar event triggered");
},
onHide:function () {
console.log("Extn:: Hide sidebar event triggered");
}
}
});
});但是,如果使用 HTML 侧栏属性加载侧边栏的,则此解决方案将无法工作,因为扩展不会在此上下文中运行。但是,您可能可以利用您引用的StackOverflow线程中描述的方法与父窗口(这将是特定于浏览器的)进行通信,这些方法反过来可以使用我们的CrossriderAPI事件与扩展进行通信。
免责声明:我是一名跨部门员工
https://stackoverflow.com/questions/20565674
复制相似问题