我有一个需要打开新窗口/标签的小书签。为了避免弹出窗口拦截器,我需要直接在书签中调用 window.open()
方法,即:在浏览器级别。
但是,我想通过加载外部 Javascript 文件来保持小书签的更新。为此,小书签需要将脚本节点附加到 DOM。如果我将 window.open()
代码放入其中一个外部加载的脚本中,弹出窗口阻止程序将从页面级别开始阻止它。
我想知道的是,我是否可以在我的小书签中围绕 window.open()
创建一个包装函数,然后从外部加载的脚本中调用它?像这样的包装的范围和权限是什么?
最佳答案
我想出了一个不完美但符合要求的解决方案:
这是书签代码:
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
只剩下一个问题:默认情况下,您要显示的页面未激活。克隆的是。
关于javascript - 是否可以从页面级脚本调用书签中定义的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2124611/