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

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

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

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

获得选择

但是当在 iFrame 中时,此方法 returns 空字符串或 getSelection 为空。尝试通过父 window (parent.getSelection()) 获取选择会导致错误(可能是由于同源策略):

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

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

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

attachTo: ['top']

感谢 Christos Papoulas! (见问题下的评论)