Opening a URL in current tab/window from a Firefox Extension

后端 未结 4 1696
生来不讨喜
生来不讨喜 2021-02-04 09:26

I am creating a Firefox Extension...what would be the javascript to open a URL in the current tab from a menuitem?

e.g. in my overlay.xul file i have the following line:

相关标签:
4条回答
  • 2021-02-04 09:41

    From a menuitem you can use openUILinkIn. It works like:

    openUILinkIn(url, where);
    

    where can be: tab, current, window (and a few other seldom used options)

    If you want to behave differently based on what keyboard modifiers a user is pressing, you can use another function whereToOpenLink, which returns tab/current/window based on the users preferences and modifiers.

    openUILinkIn(url, whereToOpenLink(event));
    

    Thus I use:

    <menuitem label="Visit homepage" 
              oncommand="openUILinkIn('http://example.com/', whereToOpenLink(event))"/>
    

    If you aren't in the context of a menuitem you might want to check out another built-in XBL that adds linking and opening HREFs for a label:

    <label value="google" class="text-link" href="http://google.com/" />
    
    0 讨论(0)
  • 2021-02-04 09:41
    <menuitem label="Visit Report Site" oncommand="var win = Components.classes['@mozilla.org/appshell/window-mediator;1']
    .getService(Components.interfaces.nsIWindowMediator)
    .getMostRecentWindow('navigator:browser'); win.openUILinkIn('http://www.google.com', 'tab');"/>
    

    Open URL in new tab.

    0 讨论(0)
  • 2021-02-04 09:47

    Call this JS functions on your commmand

    //open a url current window:
    function openUrl(url) {
    content.wrappedJSObject.location = url;
    newTabBrowser = gBrowser.selectedBrowser;
    newTabBrowser.addEventListener("load", highlight, true);
    }
    
    //new tab
    function openUrlNewTab(url) {
    var win = Components.classes['@mozilla.org/appshell/window-mediator;1']
                .getService(Components.interfaces.nsIWindowMediator)
                .getMostRecentWindow('navigator:browser');
    win.gBrowser.selectedTab = win.gBrowser.addTab(url);
    }
    
    0 讨论(0)
  • 2021-02-04 09:52

    After browsing around, I found that I had to replace the above code with this:

    <menuitem label="Visit homepage" oncommand="content.wrappedJSObject.location='http://www.somepage.com'"/>
    
    0 讨论(0)
提交回复
热议问题