当用户打开新选项卡时,显示指定URL的页面,而不是默认页面或空白页面

时间:2014-09-22 23:33:05

标签: google-chrome firefox google-chrome-extension firefox-addon firefox-addon-sdk

我正在尝试将简单的Chrome扩展程序移植到Firefox附加组件/扩展程序。 Chrome扩展程序的代码为:

的manifest.json

{
  ...
  "background": {
    "scripts": [
      "javascripts/background.js"
    ]
  },
  "chrome_url_overrides": {
    "newtab": "main.html"
  },
  "permissions": [
    "tabs"
  ]
}

main.html中

<html>
  <head></head>
  <body style="margin: 0; padding: 0; overflow: none;">
    <iframe src='http://google.com' width='100%' height='100%' frameborder='0' seamless></iframe>
  </body>
</html>

Javascript角/ background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.update(tab.id, { url: 'http://google.com' });
});

在Firefox中,我创建了一个Firefox SDK-addon,最后我得到了:

LIB / main.js

var buttons = require('sdk/ui/button/action');
var tabs = require('sdk/tabs');
var self = require('sdk/self');
var pageUrl = self.data.url('main.html')

var button = buttons.ActionButton({
  id: 'mozilla-link',
  label: 'Googler',
  icon: {
    '16': './images/icon16.png',
    '32': './images/icon32.png',
    '64': './images/icon64.png'
  },
  onClick: handleClick
});

function handleClick(state) {
  tabs.open('http://google.com/');
}

tabs.on('activate', function(tab) {
  tab.url = 'http://google.com/'
});

但它无法正常工作,我不希望URL显示在URL栏上。

我希望在用户打开新标签后显示指定的网站。我不介意用户是否知道实际的URL,但我认为填充URL-bar有点烦人,因为用户必须删除URL(在这种情况下是google.com)才能放入新的URL。

我希望与usepanda.com具有相同的行为。他们的扩展名覆盖了newtab页面并显示了Panda网站。显然,用户在下载扩展程序时会发现此行为。

如何实现相同的功能?我正在考虑使用XUL和覆盖,但我有点迷失。

3 个答案:

答案 0 :(得分:2)

经过一些质询,我们现在知道你想要的是:只要用户打开一个新选项卡(即用户打开为空白选项卡而不是打开的选项卡),就会打开一个特定的URL由用户跟踪链接并指定在新选项卡中打开链接):

有许多扩展已经这样做了。我建议你检查它们的代码。就个人而言,我使用的是Tab Utilities,但它的很多功能比你想要的更多。

very quick search on Mozilla Add-ons生成的New Tab Homepage非常小,似乎与overlay extension的内容非常接近。另一方面,它看起来像自定义新标签 完全你想要什么。但是,它是Restartless/Bootstraped extension,这使它更复杂。因此,我将在下面显示New Tab Homepage

如上所述,New Tab Homepage是重叠式扩展程序。我没有快速看到列出的扩展程序基于Add-on SDK。因此,这并不是您所要求的。

来自New Tab Homepage的代码:

Javascript( chrome / content / tabhomepage.js ):

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is New Tab Homepage.
 *
 * The Initial Developer of the Original Code is
 *   Ben Basson <ben@basson.at>
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Ben Basson <ben@basson.at>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

var newtabhomepage = {

  init: function ()
  {
    gBrowser.removeEventListener("NewTab", BrowserOpenTab, false);
    window.BrowserOpenTab = newtabhomepage.opentab;

    // explicitly add new listener
    gBrowser.addEventListener("NewTab", newtabhomepage.opentab, false);

    newtabhomepage.prefs = Components.classes['@mozilla.org/preferences-service;1']
                           .getService(Components.interfaces.nsIPrefService);
  },

  opentab: function (aEvent)
  {
    // Firefox allows multiple piped homepages, take the first if necessary
    var homepage = gHomeButton.getHomePage().split("|")[0];
    var newtab = gBrowser.addTab(homepage);
    if (newtabhomepage.prefs.getBoolPref("newtabhomepage.selectnewtab"))
    {
      gBrowser.selectedTab = newtab;
      if (gURLBar)
        setTimeout(function() { 
          // if page is about:blank select() works just like focus, two birds one stone
          gURLBar.select();
        }, 0);
    }
    if (aEvent)
      aEvent.stopPropagation();

    return newtab;
  }

}

window.addEventListener("load",newtabhomepage.init,false);

要获得您想要的内容,您可以编辑JavaScript以使URL的分配成为静态的。

XUL叠加层( chrome / content / tabhomepage.xul ):

<?xml version="1.0"?>
<!-- 
  ***** BEGIN LICENSE BLOCK *****
  Version: MPL 1.1/GPL 2.0/LGPL 2.1

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at
  http://www.mozilla.org/MPL/

  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the
  License.

  The Original Code is New Tab Homepage.

  The Initial Developer of the Original Code is
    Ben Basson <ben@basson.at>
  Portions created by the Initial Developer are Copyright (C) 2005
  the Initial Developer. All Rights Reserved.

  Contributor(s):
    Ben Basson <ben@basson.at>

  Alternatively, the contents of this file may be used under the terms of
  either the GNU General Public License Version 2 or later (the "GPL"), or
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  in which case the provisions of the GPL or the LGPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
  under the terms of either the GPL or the LGPL, and not to allow others to
  use your version of this file under the terms of the MPL, indicate your
  decision by deleting the provisions above and replace them with the notice
  and other provisions required by the GPL or the LGPL. If you do not delete
  the provisions above, a recipient may use your version of this file under
  the terms of any one of the MPL, the GPL or the LGPL.

 ***** END LICENSE BLOCK ***** -->

<overlay id="tabhomepageOverlay"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  xmlns:nc="http://home.netscape.com/NC-rdf#">

  <script type="application/x-javascript"
    src="chrome://tabhomepage/content/tabhomepage.js"/>

</overlay>

chrome.manifest用于:

overlay chrome://browser/content/browser.xul    chrome://tabhomepage/content/tabhomepage.xul
content tabhomepage chrome/content/

的install.rdf:

<?xml version="1.0"?>

<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:em="http://www.mozilla.org/2004/em-rdf#">

  <Description about="urn:mozilla:install-manifest">

    <em:id>{66E978CD-981F-47DF-AC42-E3CF417C1467}</em:id>
    <em:version>0.4.3</em:version>
    <em:name>New Tab Homepage</em:name>
    <em:description>Loads your homepage when you open a new tab.</em:description>
    <em:creator>Ben Basson</em:creator>
    <em:homepageURL>http://www.cusser.net</em:homepageURL>

    <em:targetApplication>
      <Description>
        <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
        <em:minVersion>3.0b2</em:minVersion>
        <em:maxVersion>4.0.*</em:maxVersion>
      </Description>
    </em:targetApplication>

  </Description>

</RDF>

首选项JavaScript(默认值/首选项/ tabhomepage.js ):

/* Focus newly created tabs */
pref("newtabhomepage.selectnewtab", true);

这是New Tab Homepage扩展程序的全部内容。

答案 1 :(得分:0)

使用Jetpack可以像这样重置全局:

var { get, set } = require("sdk/preferences/service");
var { when: unload } = require("sdk/system/unload");
var oldValue = get("browser.newtab.url");
set("browser.newtab.url", "http://somedomain.com" );
unload(function() {
  set("browser.newtab.url", oldValue);
});

Firefox SDK preferences/service

答案 2 :(得分:-1)

你可以这样做:

var {Cu} = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');
var newuri = Services.io.newURI('http://www.bing.com/', null, null);
Services.wm.getMostRecentWindow('navigator:browser').gBrowser.selectedTab.linkedBrowser.webNavigation.setCurrentURI(newuri);

要测试它打开暂存器,转到菜单栏,将它设置为环境到浏览器,然后运行它,它会将当前选项卡更改为bing.com