user3130841
user3130841

Reputation: 11

Page action to open new page

Can I use a page action to directly open a new page? I have a link in my popup.html, but it would be better to have the page open when they click the icon so that they would only need one click instead of two.

<!doctype html>
<html>
  <head>
    <title>Popup</title>
    <link href="popup.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <a href="http://www.domain.com/details.html" target="_blank">click here</a>
  </body>
</html>

Upvotes: 1

Views: 427

Answers (2)

Sachin Jain
Sachin Jain

Reputation: 21842

I don't think there is any elegant way to handle both the situations on page action click:

  1. Open popup
  2. Open a new page

If you always want to open a new tab with some URL whenever page action is clicked, just remove the popup. And use the code just like @Flo has mentioned.

chrome.pageAction.onClicked.addListener(function(tab) {
  chrome.tabs.create({url: "http://www.example.com", "active":true});
});

PS: To remove the popup, there are two options:

  1. Remove the popup from manifest.json
  2. Pragmatically like chrome.pageAction.setPopup('')

Upvotes: 1

flo
flo

Reputation: 983

Yes, a way to achieve this would be as follows:

 chrome.pageAction.onClicked.addListener(function(tab){

     chrome.tabs.create({url: "http://www.domain.com/details.html", "active":true});

 });  

See Chrome Page Action | onClicked

Note that you will need to declare the tabs permission in your manifest file:

"permissions": ["tabs",...],

Upvotes: 2

Related Questions