可以从页面级脚本调用小书签中定义的函数吗?
我有一个书签需要打开一个新窗口/选项卡。为了避免弹出窗口拦截器,我需要直接在书签中调用 window.open()
方法,即:在浏览器级别。
但是,我想通过加载外部 Javascript 文件来保持小书签的可更新性。为此,小书签需要将脚本节点附加到 DOM。如果我将 window.open() 代码放入这些外部加载的脚本之一中,弹出窗口阻止程序将从其页面级别开始阻止它。
我想知道的是,是否可以在书签中创建一个围绕 window.open()
的包装函数,然后从外部加载的脚本中调用它?像这样的包装的范围和权限是什么?
I have a bookmarklet that needs to open a new window/tab. In order to avoid the popup blocker, I need to call the window.open()
method directly in the bookmarklet ie: at the browser-level.
However, I want to keep the bookmarklet updatable by loading external Javascript files. To do this, the bookmarklet needs to append script nodes to the DOM. If i were to put window.open()
code in one of these externally loaded scripts, the popup blocker would block it since its page-level.
What I want to know is if I can create a wrapper function around window.open()
in my bookmarklet, then call it from the externally loaded script? What is the scope and what are the permissions on a wrap such as this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了一个并不完美但满足要求的解决方案:
这是书签代码:
可读的分步等效内容是:
只剩下一个问题:您要显示的页面默认情况下不处于活动状态。克隆的就是。
I came up with a solution which isn't perfect but meets the requirements:
Here is the bookmarklet code:
The readable step-by-step equivalent being:
Only one issue remains: the page you want to show isn't active by default. The cloned one is.
我想知道这种方法是否可行——很高兴看到它确实有效。
这里的普遍问题是,除了直接用户交互之外,浏览器不允许您打开新窗口。因此您无法从远程脚本打开该窗口。
您直接从书签打开窗口,移动到该位置,然后调用远程脚本。
我采用的替代方案是将远程脚本的内容直接移至小书签。这对于我的简单应用程序来说很好。我在我的博客上写下了
I wondered if that approach might work - good to see that it does.
The general problem here is that browsers will not let you open a new window other than by direct user interaction. So you cannot open the window from a remote script.
You are opening the window directly from the bookmarklet, moving to that location and then invoking the remote script.
The alternative, which I went with, was to move the contents of the remote script directly to the bookmarklet. That was fine for my simple application. I wrote up that on my blog