javascript - 用于取消修复滚动工具栏的脚本适用于 chrome 上的 tampermonkey,但不适用于 firefox 上的 greasmonkey
问题描述
我目前正在使用我在 github 上找到的脚本来“取消修复工具栏”(只是阻止 css 元素随页面滚动;不知道我是否使用了正确的术语)。它适用于 chrome 上的 tampermonkey(尽管脚本页面上有错误),但不适用于 Firefox 中的 greasmonkey。为什么它不适用于 Firefox 中的greasemonkey,我该如何解决?
// ==UserScript==
// @name unfix-all-the-toolbars
// @description Removes "position: fixed" style from elements, unfixing "toolbars" and the such.
// @namespace https://hasanyavuz.ozderya.net
// @include *
// @version 1
// @grant none
// ==/UserScript==
// Based on https://stackoverflow.com/questions/13696100/greasemonkey-script-to-make-fixed-positioned-elements-static
// and https://gist.github.com/vbuaraujo/bddb3b93a0b2b7e28e1b
fixed_items = [];
function unfixAll() {
Array.forEach(
document.querySelectorAll("h1, h2, ul, ol, li, div, nav, header, footer"),
function (el) {
var style = window.getComputedStyle(el);
if (style.position === "fixed" && style.top == "0px" &&
!(style.display === "none" || style.visibility === "hidden"))
/* Avoid unfixing JavaScript popups like Twitter's "confirm retweet" window */
{
fixed_items.push(el);
//el.style.position = "static";
el.style.visibility = "hidden";
/* I tried using "absolute" in the past, but this breaks WordPress's footer.
Using "static" breaks it too, but at least it doesn't get in the way. */
}
});
}
function fixBack()
{
Array.forEach(
fixed_items,
function(el) {
//el.style.position = "fixed";
el.style.visibility = "";
}
)
fixed_items = [];
}
function onScroll()
{
if (window.scrollY > 0)
{
unfixAll();
}
else
{
fixBack();
}
}
window.addEventListener("scroll", onScroll);
解决方案
它适用于 chrome 上的 tampermonkey(尽管脚本页面上有错误),但不适用于 Firefox 中的 greasmonkey。
在回答另一个脚本的查询时,我遇到了类似的问题(参考:[FireMonkey] Request YT' JS script ad blocker。
该脚本不声明变量,例如,这会在严格模式下fixed_items = [];
破坏脚本。
这是重新编写的相同代码:(我没有测试过)
// ==UserScript==
// @name unfix-all-the-toolbars
// @description Removes "position: fixed" style from elements, unfixing "toolbars" and the such.
// @namespace https://hasanyavuz.ozderya.net
// @include *
// @version 1
// @grant none
// ==/UserScript==
// Based on https://stackoverflow.com/questions/13696100/greasemonkey-script-to-make-fixed-positioned-elements-static
// and https://gist.github.com/vbuaraujo/bddb3b93a0b2b7e28e1b
(() => { // anonymous function wrapper, for error checking & limiting scope
let fixed_els = [];
function unfixAll() {
document.querySelectorAll('h1, h2, ul, ol, li, div, nav, header, footer').forEach(el => {
const style = window.getComputedStyle(el);
if (style.position === 'fixed' && style.top == '0px' &&
!(style.display === 'none' || style.visibility === 'hidden')) {
// Avoid unfixing JavaScript popups like Twitter's "confirm retweet" window
fixed_els.push(el);
el.style.visibility = 'hidden';
/* I tried using "absolute" in the past, but this breaks WordPress's footer.
Using "static" breaks it too, but at least it doesn't get in the way. */
}
});
}
function fixBack() {
fixed_els.forEach(el => el.style.visibility = '');
fixed_els = [];
}
function onScroll() {
window.scrollY > 0 ? unfixAll() : fixBack();
}
window.addEventListener('scroll', onScroll);
})();
推荐阅读
- openedge - 进度 4GL:为什么 TRIM 功能在这里无济于事?
- python - 使用 1 个列表推导而不是 3 个使用 Enumerate Python 组合两个不同的索引搜索
- angular - Angular中动态加载组件自动检测变化的方法
- mongodb - 如何对嵌套文档使用元素查询运算符 ($exists)?
- reactjs - 使用 while 循环测试 redux saga 并使用 redux-saga-test-plan 进行延迟
- html - 如何在行的中心显示所有值,并且行上方的 div 的宽度与其下方的行不同。如何做到这一点?
- javascript - webpackConfig 上的 ESM 导入
- javascript - 单击图标时从 localStorage 中删除项目及其数据
- python - 为什么我不能通过单击 .py 文件来运行 python 程序
- geometry - Revit API。如何获得多个元素的边界框?