简体   繁体   English

chrome扩展程序开发从JavaScript关闭打开的窗口

[英]chrome extension development close openned window from javascript

When developing a chrome extension I managed to open a new tab by using this code in my javascript file 开发chrome扩展程序时,我设法通过在JavaScript文件中使用此代码来打开新标签页

myWindow=window.open("www.google.com");

I could close it immediately after opening it with: 我可以在打开它后立即关闭它:

myWindow.close();

I tried several methods for javascript for it to wait some seconds before closing but if I do that it doesn't close. 我尝试使用javascript的几种方法,使其在关闭之前需要等待几秒钟,但如果这样做,它不会关闭。 Maybe it's because it loses the window's id? 也许是因为丢失了窗口的ID? I don't know. 我不知道。 I just started learning about chrome extension development. 我刚刚开始学习chrome扩展程序开发。

[EDIT] I am submitting all of the code I have to help you guys. [编辑]我正在提交所有必须帮助您的代码。 I remind you that I am trying to develop a chrome extension and the action happens when I click on the button on my toolbar created by my extension. 提醒您,我正在尝试开发chrome扩展程序,当我单击由扩展程序创建的工具栏上的按钮时,便会执行该操作。 (Code taken from here: http://developer.chrome.com/extensions/getstarted.html ) (代码来自此处: http : //developer.chrome.com/extensions/getstarted.html

HTML FILE HTML文件

<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
  body {
    min-width: 357px;
    overflow-x: hidden;
  }

  img {
    margin: 5px;
    border: 2px solid black;
    vertical-align: middle;
    width: 75px;
    height: 75px;
  }
  </style>
 <script src="popup.js"></script>
</head>
<body>
</body>
</html>

MANIFEST 表现

{
"manifest_version": 2,

"name": "myExtension",
"description": "This extension is under development",
"version": "1.0",

"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["www.google.com"]
}

You seem to be doing this from the extension's popup. 您似乎正在从扩展程序的弹出窗口中执行此操作。 Popups are closed when they loose focus, so my guess is that what's happening is that its execution environment is being destroyed when your new window gets the focus. 当弹出窗口失去焦点时,它们就会关闭,因此我猜测是,当您的新窗口获得焦点时,弹出窗口的执行环境正在被破坏。 So any delayed event you set up there can't be executed. 因此,您在此处设置的任何延迟事件都无法执行。

It seems your popup is an empty window and the only thing it does is opening the window. 看来您的弹出窗口是一个空窗口,唯一要做的就是打开窗口。 In that case you don't need to define a popup. 在这种情况下,您无需定义弹出窗口。 You can do that from a background page using the chrome.browserAction.onClicked event . 您可以使用chrome.browserAction.onClicked事件从后台页面执行此chrome.browserAction.onClicked

If you do need to start the action from your popup, you can define a function in your background page, and invoke it using chrome.runtime.getBackgroundPage(function(w){w.yourFunction();}) . 如果确实需要从弹出窗口开始操作,则可以在后台页面中定义一个函数,然后使用chrome.runtime.getBackgroundPage(function(w){w.yourFunction();})调用它。

Also, keep in mind that in an extension you have the chrome.windows and chrome.tabs APIs available. 另外,请记住,在扩展程序中,您可以使用chrome.windowschrome.tabs API。

This can't be delayed due to the browsers security framework. 由于浏览器的安全框架,这不能被延迟。 To open/close a window a user initiated event has to happen beforehand. 要打开/关闭窗口,必须事先发生用户启动的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM