iFrame中的SelectionContext-访问属性“getSelection”的权限被拒绝

SelectionContext in iFrame - Permission denied to access property ‘getSelection’

本文关键字:权限 拒绝 getSelection 中的 SelectionContext- 访问 属性 iFrame      更新时间:2023-09-26

在带有iFrame的网站上,如果我通过上下文菜单内容脚本绕行,我很难检索所选文本。

在main.js中,我用SelectionContext定义了一个上下文菜单项,在其onMessage-方法中,会通知page-mod contentScriptFile。然后,这个脚本应该通过getSelection().toString()获得选择。

但是,当位于iFrame内部时,此方法要么返回空字符串,要么getSelection为null。试图通过父窗口(parent.getSelection())获取选择结果导致错误(可能是由于同源策略):

console.error:消息:错误:访问属性"getSelection"的权限被拒绝

有人知道如何解决这个问题吗?Thanx

指定attachTo-方法是我需要的解决方案:

attachTo: ['top']

感谢Christos Papoulas!(请参阅问题下方的评论)