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

蹲街弑〆低调 提交于 2019-12-23 13:03:21

问题


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


回答1:


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.




回答2:


Although this is an old question, I'll post the working code, as of 2018.

To open anchor links from Chrome extension in the same window, place this code in your popup.js:

var links = document.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        (function () {
            var ln = links[i];
            var location = ln.href;
            ln.onclick = function () {
                chrome.tabs.update({active: true, url: location});
            };
        })();
    }



回答3:


This may be a simple fix if I understand you correctly;

For each of the links that you want to open in the same tab, use this HTML:

<a href="LINK" target="_self">Link Name</a>

A simpler way of writing this is to do:

<a href="LINK">Link Name</a>

Hope I was of help :)



来源:https://stackoverflow.com/questions/15245577/chrome-extension-how-do-i-open-urls-in-popup-html-in-the-same-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!