Greasemonkey脚本使固定定位元素静态

时间:2022-04-03 06:13:13

I find elements on a web page that have been positioned fixed to be in my way frequently. I'd like to find a way to disable position: fixed CSS rules in any web site that I visit.

我发现网页上的元素已被定位,以便经常使用我的方式。我想找到一种禁用位置的方法:在我访问的任何网站中修复CSS规则。

I wrote a userscript (Firefox, Greasemonkey) that scans every node in the document and figures out if it has a computed style position fixed, then overrides that to be static.

我编写了一个用户脚本(Firefox,Greasemonkey),它扫描文档中的每个节点,并确定它是否已修复计算出的样式位置,然后将其覆盖为静态。

Is there a better way to accomplish my goal?

有没有更好的方法来实现我的目标?

This is the script I wrote, I've narrowed it to just divs for now:

这是我写的剧本,我现在把它缩小到只有div:

Array.forEach(
    document.querySelectorAll("div")
    ,function(el) {
        if (window.getComputedStyle(el).position === 'fixed') {
            el.style.position = 'static';
        }
    }
);

1 个解决方案

#1


5  

If your Greasemonkey script works, it is probably the most cost effective way to eliminate fixed-positioned styling.

如果您的Greasemonkey脚本有效,它可能是消除固定定位样式的最具成本效益的方法。

Some alternatives that require much more effort but will use less CPU/memory per page:

一些替代方案需要更多的努力,但每页使用更少的CPU /内存:

  1. Write an Add-on that:

    写一个附加组件:

    1. Deletes CSS style rules as they are loaded. (Greasemonkey cannot always do this because of cross-domain issues.)
    2. 在加载CSS样式规则时删除它们。 (由于跨域问题,Greasemonkey不能总是这样做。)

    3. Uses Mutation Observers to intercept javascript attempts to set position: fixed.
    4. 使用Mutation Observers拦截javascript尝试设置position:fixed。


  2. Fork and compile your own version of Firefox that ignores position: fixed. You'd probably want this controlled by both a "blacklist" and a "whitelist".

    fork并编译自己的Firefox版本,忽略position:fixed。您可能希望由“黑名单”和“白名单”控制。

#1


5  

If your Greasemonkey script works, it is probably the most cost effective way to eliminate fixed-positioned styling.

如果您的Greasemonkey脚本有效,它可能是消除固定定位样式的最具成本效益的方法。

Some alternatives that require much more effort but will use less CPU/memory per page:

一些替代方案需要更多的努力,但每页使用更少的CPU /内存:

  1. Write an Add-on that:

    写一个附加组件:

    1. Deletes CSS style rules as they are loaded. (Greasemonkey cannot always do this because of cross-domain issues.)
    2. 在加载CSS样式规则时删除它们。 (由于跨域问题,Greasemonkey不能总是这样做。)

    3. Uses Mutation Observers to intercept javascript attempts to set position: fixed.
    4. 使用Mutation Observers拦截javascript尝试设置position:fixed。


  2. Fork and compile your own version of Firefox that ignores position: fixed. You'd probably want this controlled by both a "blacklist" and a "whitelist".

    fork并编译自己的Firefox版本,忽略position:fixed。您可能希望由“黑名单”和“白名单”控制。