如何使用WebExtensions更改背景图像?

时间:2017-08-17 16:09:38

标签: javascript firefox firefox-webextensions

我想用WebExtension更改Firefox新选项卡(about:newtab)的背景图片。我已经尝试过这段代码,但它不起作用:

window.addEventListener("load",function(){
   if(window.document.URL == "about:newtab"){
       window.document.body.style.backgroundImage = "url(https://images-assets.nasa.gov/image/iss050e066094/iss050e066094~orig.jpg)"
   }
});

的manifest.json:

{
  "description": "An example extension",
  "homepage_url": "",
  "manifest_version": 2,
  "name": "wallpaper",
  "permissions": [
    "alarms",
    "theme",
    "<all_urls>"
  ],
  "background": {
    "scripts": ["background.js"]
  },
  "version": "1.0",
  "gecko": {
      "strict_min_version": "55.0a2"
  }
}

提前致谢。

1 个答案:

答案 0 :(得分:2)

目前,由于安全原因,您无法更改:页面。

如果您真的想要about:newtab上的其他背景图片,则需要使用chrome_url_overrides覆盖新标签页,并使用您自己的实现。自Firefox 54以来,可以使用Newtab覆盖(在 Bug 1234150 中实现)。

你会这样做:

"chrome_url_overrides" : {
  "newtab": "my-new-tab.html"
}

所以你的清单会变成这样的东西

{
  "description": "An example extension",
  "homepage_url": "",
  "manifest_version": 2,
  "name": "wallpaper",
  "permissions": [
    "alarms",
    "theme",
    "<all_urls>"
  ],
  "background": {
    "scripts": ["background.js"]
  },
  "version": "1.0",
  "gecko": {
      "strict_min_version": "55.0a2"
  },
  "chrome_url_overrides" : {
    "newtab": "my-new-tab.html"
  }
}

实现自己的自定义新标签页是一项非常重要的任务:

  • 您需要实施热门访问过的网站。您可以使用topSites api
  • 执行此操作
  • 可能你也希望实现搜索。由于您无法读取搜索引擎(Bug 1352598),因此您可能希望通过插件中的硬编码网址实现搜索,添加下拉列表以选择您喜欢的搜索引擎。接下来,当用户输入查询并按下ENTER时,您可以替换当前的&#34;新选项卡&#34;使用tabs.update方法搜索结果页面的页面,将url属性替换为搜索页面+查询的网址。

我已经打开了一个错误报告,要求提供一个API来设置about:newtab和about:home的背景图片。请参阅Bug 1391912

相关问题