可以从页面级脚本调用小书签中定义的函数吗?

发布于 2024-08-18 22:00:58 字数 309 浏览 8 评论 0原文

我有一个书签需要打开一个新窗口/选项卡。为了避免弹出窗口拦截器,我需要直接在书签中调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2024-08-25 22:00:58

我想出了一个并不完美但满足要求的解决方案:

这是书签代码:

javascript:window.open(window.location);window.location="http://www.google.com/";var%20s=document.createElement('script');s.setAttribute('src','http://my-script.js');document.body.appendChild(s);void(0);

可读的分步等效内容是:

window.open(window.location);                // Clone the current tab
window.location = "http://www.google.com/";  // Navigate to the desired page url
var s = document.createElement('script');    // Create the script
s.setAttribute('src','http://my-script.js'); //
document.body.appendChild(s);                // Embed it into current document

只剩下一个问题:您要显示的页面默认情况下不处于活动状态。克隆的就是。

I came up with a solution which isn't perfect but meets the requirements:

Here is the bookmarklet code:

javascript:window.open(window.location);window.location="http://www.google.com/";var%20s=document.createElement('script');s.setAttribute('src','http://my-script.js');document.body.appendChild(s);void(0);

The readable step-by-step equivalent being:

window.open(window.location);                // Clone the current tab
window.location = "http://www.google.com/";  // Navigate to the desired page url
var s = document.createElement('script');    // Create the script
s.setAttribute('src','http://my-script.js'); //
document.body.appendChild(s);                // Embed it into current document

Only one issue remains: the page you want to show isn't active by default. The cloned one is.

醉生梦死 2024-08-25 22:00:58

我想知道这种方法是否可行——很高兴看到它确实有效。

这里的普遍问题是,除了直接用户交互之外,浏览器不允许您打开新窗口。因此您无法从远程脚本打开该窗口。

您直接从书签打开窗口,移动到该位置,然后调用远程脚本。

我采用的替代方案是将远程脚本的内容直接移至小书签。这对于我的简单应用程序来说很好。我在我的博客上写下了

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文