Firefox 扩展 - 新标签 - 如何覆盖首选项?

Firefox Extension - New Tab - How To Override Preferences?

我是 Firefox 扩展的新手,这就是我使用 Add On SDK 的原因。

我想创建一个扩展程序,在用户每次打开新标签页时显示一个特定的网站。到目前为止,这是我的代码:

var self = require("sdk/self");
var tabs = require("sdk/tabs");

// Listen for tab openings.
tabs.on('open', function onOpen(tab) {
   getActiveTab();
});

function getActiveTab(){
tabs.on('activate', function (tab) {
  tab.url = "http://www.example.com";
});
}

这行得通。但在加载指定域之前,它会加载 Firefox 默认的 newtab 页面。现在有一个 API 参考来访问新标签设置并更改为 example.com?

谢谢,

葛德

可以使用 SDK 更改 about:newtab URL:

require('sdk/preferences/service').set('browser.newtab.url', 'http://www.whosebug.com');

但它在 FF41 中变得 过时,因为不再有 browser.newtab.url 偏好。


如果您仍打算使用它,您也可以考虑将此添加到您的代码中:

var { when: unload } = require('sdk/system/unload');
var reason;
unload( function ( reason ) {
  require('sdk/preferences/service').set('browser.newtab.url', 'about:newtab');
});

以便首选项更改在加载项卸载后被撤消。您还可以将卸载原因之一传递给函数:'uninstall''disable''shutdown''upgrade''downgrade',或者不提供 reason 完全没有参数/保留未定义。

由于 browser.newtab.url 首选项已被删除,这是执行此操作的新方法:https://github.com/sblask/firefox-open-tabs-next-to-current/blob/master/lib/helpers.js#L50 The code of the module can be found here: https://dxr.mozilla.org/mozilla-central/source/browser/modules/NewTabURL.jsm

如果您还想替换主页,则必须更改 browser.startup.homepage 首选项。