I am writing my first chrome extension, which should open an URL in a new tab and after that do something.
manifest:
{
"manifest_version": 2,
"name": "Test",
"description": "",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"activeTab"
]
}
script.js:
function toggle() {
chrome.tabs.create({ url: "" }, function(tab) {
alert("Hello!");
});
}
document.getElementById('toggle').addEventListener('click', toggle);
popup.html:
<html>
<head>
</head>
<body>
<div id="toggle" class="clickable">START</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
The problem is that nothing happens after the URL is opened. What can be the problem?
I am writing my first chrome extension, which should open an URL in a new tab and after that do something.
manifest:
{
"manifest_version": 2,
"name": "Test",
"description": "",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"activeTab"
]
}
script.js:
function toggle() {
chrome.tabs.create({ url: "http://google." }, function(tab) {
alert("Hello!");
});
}
document.getElementById('toggle').addEventListener('click', toggle);
popup.html:
<html>
<head>
</head>
<body>
<div id="toggle" class="clickable">START</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
The problem is that nothing happens after the URL is opened. What can be the problem?
Share Improve this question edited May 7, 2016 at 16:43 Xan 77.7k18 gold badges197 silver badges217 bronze badges asked May 7, 2016 at 14:45 andrzej1_1andrzej1_1 1,1933 gold badges24 silver badges39 bronze badges 4- Did the popup close after the tab was created? – Rob W Commented May 7, 2016 at 15:35
- @RobW yes, it is closing – andrzej1_1 Commented May 7, 2016 at 15:39
- 2 Then why are you surprised that the script stopped running? If you want the script to continue running, run it from the background page. – Rob W Commented May 7, 2016 at 15:39
- @RobW This is the correct answer! I did not know I should call background function, thanks for help. – andrzej1_1 Commented May 7, 2016 at 15:47
1 Answer
Reset to default 8When you create a new tab, by default it opens focused, and that causes the popup to close. When the popup closes, its JavaScript context is destroyed and there's no callback left to call.
You have 2 options:
Move logic to the background/event page, which will survive the popup being closed. For example, instead of opening the tab from the popup, you can message the background page to do it for you.
Specify that you want to open the tab unfocused:
chrome.tabs.create( { url: "http://google.", active: false }, function(tab) { /* ... */ } );
However, this won't simply cause the tab to not focus - Chrome won't switch to it (opening it in the background). Might not be what you want. You can switch after your other operations are done though - with
chrome.tabs.update
to setactive: true
.
Note that there is no way for a popup to survive focus loss, this is by design.