使用 Greasemonkey 关闭窗口
我一直在为 Chrome 编写一个 Greasemonkey 脚本(拖放作为扩展),以便与 Facebook 一起使用。我知道 Greasemonkey 是沙盒的,至少就 Firefox 而言,除非更改浏览器中的设置,否则无法使用脚本关闭窗口。但是,我知道大多数浏览器允许子窗口被父窗口关闭,而不需要用户的确认。不管怎样,我试图让我的脚本打开一个窗口,从 HTML 对象中获取一段文本,然后按页面上的按钮关闭窗口。
我打开窗口的代码如下所示:
function birthday(linkAddress) {
var winNew=window.open(linkAddress, "_blank", "height=100", "width=100");
/*code to be run on page*/
winNew.close()
}
窗口打开正常,但我在 Chrome 中收到 JavaScript 错误,提示“无法调用未定义的‘close’方法”。我认为我的对象存在与 Greasemonkey 沙箱相关的问题,但我无法确定问题所在。是否可以在 Chrome 中使用 Greasemonkey 脚本关闭窗口?我需要启用某些设置吗?或者我的代码是错误的?请记住,此代码是从主窗口页面中的函数运行的,我使用写入页面的按钮的 onClick 事件将该函数设置为等于脚本中的函数。此外,任何有关访问子页面上 DOM 元素的提示也将不胜感激。
感谢您的帮助!
(抱歉,我是新手,缺乏编程实践,这是我第一次编写 Greasemonkey 脚本)
I've been writing a Greasemonkey script for Chrome (drags and drops as an extension) for use with Facebook. I know that Greasemonkey is sandboxed and that, at least as far as Firefox goes, you cannot close windows with scripts unless you change a setting in the browser. However, I know that most browsers allow child windows to be closed by the parent window without needing the confirmation of the user. Anyways I'm trying to get my script to open a window, grab a text piece from an HTML object and close the window at the press of a button on the page.
My code for opening the window looks like this:
function birthday(linkAddress) {
var winNew=window.open(linkAddress, "_blank", "height=100", "width=100");
/*code to be run on page*/
winNew.close()
}
The window opens fine but I get a javascript error in Chrome saying "cannot call method 'close' of undefined". I assume there is something wrong with my object related to Greasemonkey sandboxing, but I'm at a loss to determine what. Is it even possible to close a window using a Greasemonkey script in Chrome? Is there some setting I need to enable? Or is my code just wrong? Keep in mind that this code is running from a function in the main window page that I set equal to a function in my script using an onClick event of a button I wrote to the page. Also any tips for accessing DOM elements on the child page would be appreciated.
Thanks for any help!
(Sorry I'm quite new as well as out of practice with programming, and this is the first time I've written a Greasemonkey script)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发布的代码片段不充分,并且与“按页面上的按钮关闭窗口”的规定操作不匹配。
因此,最好的猜测是,关闭函数位于
birthday()
之外(其中定义了winNew
),或者未显示的代码中的某些内容导致了问题。另一个需要注意的是,您需要注意 Greasemonkey 脚本也可能在子窗口上触发 - 取决于子窗口打开时是否指定了 URL/什么 URL。
以下是通过按下按钮打开和关闭子窗口的示例代码:
在 jsfiddle 上查看实际情况。
The snippet of code posted is inadequate and does not match the stated operation of "close the window at the press of a button on the page".
So the best guess is that either the close function is outside
birthday()
(wherewinNew
is defined) or something in the unshown code is causing the problem.Another caveat is you need to be aware that the Greasemonkey script may also fire on the child window -- depending on if/what URL is specified when the child window is opened.
Here is sample code that opens and closes a child window at the push of buttons:
See it in action at jsfiddle.