When opening a new tab with a new tab page replacement add-on, how to keep the address bar clear?

寵の児 提交于 2020-01-13 03:41:50

问题


I'm developing a new tab page replacement add-on for Firefox. After it's installed, when I click the new tab icon to open a new tab, the new tab opens fine, but the address bar shows a messy URL: resource://firefox_p-at-getblog-dot-com/getblog_buttons_page/data/newtab.html

How do I make the address bar empty?


回答1:


There are only a few pages that will have "blank" uris in the URI bar.

Now, you have a couple of options:

  • Overlay/inject into browser.xul and just add your add-on URI to gInitialPages Or combine the about module and gInitialPages stuff.
  • Implement an about module like @canuckistani suggests. While this will still display an URI, it will be a much nicer one. ;)
  • Or, as a last resort, override about:newTab. This can (will?) have side-effects, however, like other add-ons still overlaying about:newTab which now happens to be the new page from your add-on, which might or might not work at all and/or break stuff.



回答2:


There's a function called setCurrentURI on the webNavigation/docShell object. It's a funky one, I never found a real use for it. But it does exactly what you need.

gBrowser.webNavigation.setCurrentURI(Services.io.newURI('about:blank', null, null))

setting it to about:blank blanks it out, you can set it to any url though, it won't change the page, just the url.

so listen to page DOMContentLoaded of that new tab, and as soon as it loads run this code. maybe can change it even faster, like detect when the value of the input box changes.

I couldn't figure out how to make it set the url to actually read about:newtab, maybe someone else can help us, it seems if you set it to about:ANYTHING it just blanks the url bar IF ANYTHING is already taken by an about url, so like about:newtab, about:about blanks it. It's weird maybe someone can help us figure out how to spoof that I'm curious too.




回答3:


You probably want to replace that with your own custom about: protocol - here are soem docs on how to do this:

https://developer.mozilla.org/en-US/docs/Custom_about:_URLs

Here's a real-world example of this in a fairly complex SDK-addon: https://github.com/mixedpuppy/about-awards/blob/master/lib/main.js#L171-L192



来源:https://stackoverflow.com/questions/23668805/when-opening-a-new-tab-with-a-new-tab-page-replacement-add-on-how-to-keep-the-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!