We're calling on all EU-based Mozillians with iOS or iPadOS devices to help us monitor Apple’s new browser choice screens. Join the effort to hold Big Tech to account!

为提升您的使用体验,本站正在维护,部分功能暂时无法使用。如果本站文章无法解决您的问题,您想要向社区提问的话,请到 Twitter 上的 @FirefoxSupport 或 Reddit 上的 /r/firefox 提问,我们的支持社区将会很快回复您的疑问。

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

Hide or show elements of a webpage with a hotkey?

  • 6 个回答
  • 1 人有此问题
  • 4 次查看
  • 最后回复者为 heday33967

more options

Hello. I’ve recently made the switch to Firefox due to its deep UI customizability and its open ended scripting capabilities.

I find myself using browser hosted “Apps” more and more these days and have recently realised they are open to being customised through developer tools. Hiding elements such as a side bar or a persistent website header you don’t really care for.

I am wondering is it possible to automate this processes? So instead of opening the developer tools every time you visit a site, instead you configure it once and then its automatically hidden from view.

Secondly, would it be possible to trigger the hiding of elements through a hotkey? So for example you can show or hide a side bar just when you need it?

Just trying to get consensus on what is possible or not and see which direction I need to go. Thanks!

Hello. I’ve recently made the switch to Firefox due to its deep UI customizability and its open ended scripting capabilities. I find myself using browser hosted “Apps” more and more these days and have recently realised they are open to being customised through developer tools. Hiding elements such as a side bar or a persistent website header you don’t really care for. I am wondering is it possible to automate this processes? So instead of opening the developer tools every time you visit a site, instead you configure it once and then its automatically hidden from view. Secondly, would it be possible to trigger the hiding of elements through a hotkey? So for example you can show or hide a side bar just when you need it? Just trying to get consensus on what is possible or not and see which direction I need to go. Thanks!

被采纳的解决方案

The bookmarklet I posted above works as a toggle as it checks the current display state (defaultView.getComputedStyle). You click it once to run the code to hide the element specified by the selector, in this case ".questions-sidebar". You click the bookmarklet another time to run the code once again and it will re-display this element. This only works if you run the bookmarklet on the page where you want to hide this specific content and this is similar to running JavaScript code in the Web Console. This code works in the Web Console, but it is more convenient to have such a bookmarklet available as a button on the Bookmarks Toolbar.

Using code in userContent.css is only convenient if you want to hide specific content permanently although in some case it is possible to unhide the content via a bookmarklet (only possible if the !important flag isn't used) or by using the Inspector to override rule(s) in userContent.css.

定位到答案原位置 👍 1

所有回复 (6)

more options

Hi, we're using uBlock Origin for hiding things.

more options

You can alternatively use rules in userContent.css to hide content on specific domains or pages or otherwise modify the appearance.

An example with code in the userContent.css file.


@-moz-document domain(support.mozilla.org){
 .questions-sidebar {display: none !important;} /* hide sidebar*/
}

In Firefox 69 and later you need to set this pref to true on the about:config page to enable userChrome.css and userContent.css in the chrome folder.

  • toolkit.legacyUserProfileCustomizations.stylesheets = true

See:

more options

Another possibility is to use a JavaScript bookmarklet to toggle a specific element.

javascript:/*toggle style*/(function(){
var d=document,s=[{s:'.questions-sidebar',o:0}],aP=[{p:'display',t:'none',v:'block',i:1},{p:'visibility',t:'hidden',v:'visible',i:1}],pP,i,j,n,N,S;
for(j=0;S=s[j];j++){
pP=aP[S.o];n=d.querySelectorAll(S.s);for(i=0;N=n[i];i++){
N.style.setProperty(pP.p,((d.defaultView.getComputedStyle(N,'').getPropertyValue(pP.p))==pP.t)?pP.v:pP.t,pP.i?'important':'');
}}
})()

more options

Guys, thank allot for your help

Cor-el Thank you so much with your two suggestions. I have been using pre configured CSS files to mostly controll Firefox/Thunderbird appearances, So its great to know that you can extend it to control web content as well. I definetly plan to learn more about it now.

Your first code example is excellent but I have a follow up question to your second one. I did a qucik rudimentary read up on bookmarklets, apparently they are Javascript code you add to a URL before hitting enter

Do bookmarklets require to reload the page in order to toggle the elements visibility?

Lastly, Can bookmarklets be excuted outside of a browser, say from a folder on File explorer just like you would execute a .url file? I have allot more controll over windows with scripting language such as Authotkey and some Python as well. So it would be really nice to know.

由heday33967于修改

more options

选择的解决方案

The bookmarklet I posted above works as a toggle as it checks the current display state (defaultView.getComputedStyle). You click it once to run the code to hide the element specified by the selector, in this case ".questions-sidebar". You click the bookmarklet another time to run the code once again and it will re-display this element. This only works if you run the bookmarklet on the page where you want to hide this specific content and this is similar to running JavaScript code in the Web Console. This code works in the Web Console, but it is more convenient to have such a bookmarklet available as a button on the Bookmarks Toolbar.

Using code in userContent.css is only convenient if you want to hide specific content permanently although in some case it is possible to unhide the content via a bookmarklet (only possible if the !important flag isn't used) or by using the Inspector to override rule(s) in userContent.css.

more options

Cor-el I am intrested both in toggling and hidding permanently. Thanks allot for fleshing this out for me. Now I know which many routes are available to me as I learn more about CSS and JS.