Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension: How do i open urls in popup.html in the same tab

Google Chrome extension:

i am running mad on a simple thing. Please dont blame me, i am not english origin and i have trouble to read and understand all the extension docs.

I simply want to do the following:

I have lets say 8 different URLs in my popop.html which opens when i click on my icon in the top right browserbar.
(url) example.com
(url) Other Example and etc ...

clicking an url does nothing, target="_blank" opens always a new tab but i want to open them all in the same tab when clicking on one. target="_top" or target="_self" doesnt seem work here.

I have no idea how to program this. I come rather from php and i need a quick solution.

Has anyone a litte ready (easy to understand) code snippet or an Idea how to open my urls in the same tab ? (where to place what) ( i understood the manifest definitions so far, but all this java up and down is too difficult to understand for me at the moment only for this "simple" task).

Thanks in Advance RJ

like image 957
user2139421 Avatar asked Mar 06 '13 11:03

user2139421


People also ask

How do I get links to open in the same window in Chrome?

How do I get links to open in the same window in Chrome? It's the Chrome Toolbox (by Google) extension you are looking for. You can choose to open in front or background tab. I personally Ctrl+Click when I need to open a link in a new tab.

How do I open a link in the same tab?

For opening the new link in the same tab or window, we can use following code. window. open("www.google.com","_self"); By using above code, we can open a link in the same tab/window.

How do I make Chrome open pop up tabs?

Right click in titlebar of popup window and select "Show As Tab". Once popup is transformed in a window with tab, you can drag it back to main window.


1 Answers

I believe that chrome does not allow the popups to open an external page by any way. The only solution I know is to place an iframe in your popup.html file with src attribute set to popup2.html and place all your html inside popup2.html. However, consider that all websites do not work well inside iframe.

If you are trying to open url in currently active tab then try following:

Attach following script to your popup.html file:

var hrefs = document.getElementsByTagName("a");

function openLink() {
    var href = this.href;
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        var tab = tabs[0];
        chrome.tabs.update(tab.id, {url: href});
    });
}

for (var i=0,a; a=hrefs[i]; ++i) {
    hrefs[i].addEventListener('click', openLink);
}

You need to add tabs permisson to your manifest file for this to work.

like image 195
Uzair Farooq Avatar answered Sep 30 '22 20:09

Uzair Farooq