【问题标题】:Display the page of a specified URL, instead of the default or blank page, when the user opens a new tab当用户打开新选项卡时,显示指定 URL 的页面,而不是默认页面或空白页面
【发布时间】:2014-09-22 23:33:05
【问题描述】:

我正在尝试将一个简单的 Chrome 扩展程序移植到 Firefox 插件/扩展程序。 Chrome 扩展的代码是:

ma​​nifest.json

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

ma​​in.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>

javascripts/background.js

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

在 Firefox 中,我创建了一个 Firefox SDK 插件,最终得到:

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 栏有点烦人,因为用户必须删除 URL(在本例中为 google.com)才能放置一个新 URL。

我希望拥有与 usepanda.com 相同的行为。他们的扩展覆盖新标签页并显示熊猫网站。显然,用户在下载扩展程序时已经意识到了这种行为。

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

【问题讨论】:

  • 有多种方法可以做到这一点。但是,在 URL 栏中显示实际 URL 被认为是一个安全问题。为什么要让用户觉得他们在 google.com 或其他网站,而实际上他们不在?
  • 你到底想做什么?
  • @Xan 我希望拥有与 usepanda.com 相同的行为。他们的扩展覆盖新标签页并显示熊猫网站。显然,用户在下载扩展程序时已经意识到了这种行为。
  • 哦,没关系,这个问题是针对制作 Firefox 插件的。我看错了。
  • @Makyen 我想在用户打开新标签后显示给定网站。我不介意用户是否知道实际的 URL,但我认为填充 URL 栏有点烦人,因为用户必须删除 URL(在本例中为 google.com)才能放置一个新 URL。感谢您的帮助。

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


【解决方案1】:

经过一番询问,我们现在知道您想要的是: 确保每当用户打开一个新选项卡(即用户以空白选项卡打开的选项卡,而不是打开的选项卡)时都会打开一个特定的 URL由用户点击链接并指定在新选项卡中打开链接):

已经有很多扩展可以做到这一点。我建议你检查他们的代码。就我个人而言,我使用Tab Utilities,但它的功能比您想要的要多得多

very quick search on Mozilla Add-ons 生成了 New Tab Homepage,它非常小,看起来与您想要的 overlay extension 非常接近。另一方面,自定义新标签看起来正是你想要的。但是,它是一个Restartless/Bootstraped extension,这使得它更加复杂。因此,我将在下面显示New Tab Homepage

如前所述,New Tab Homepage 是一个覆盖扩展。我没有很快看到列出的基于附加 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/

安装.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(defaults/preferences/tabhomepage.js):

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

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

【讨论】:

    【解决方案2】:

    使用 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

    【讨论】:

      【解决方案3】:

      你可以这样做:

      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

      【讨论】:

        猜你喜欢
        • 2019-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-08
        • 1970-01-01
        • 2014-02-14
        • 1970-01-01
        相关资源
        最近更新 更多