// ==UserScript==
// @name NodeLoc 收起右栏 · 正文铺平居中
// @namespace https://www.nodeloc.com/
// @version 1.0.0
// @description 一键收起 NodeLoc 右侧信息栏(近期热门/广告/目录等),让帖子列表和正文铺开并居中显示;可随时恢复,状态自动记忆
// @author mjjjjjjjjj
// @match https://www.nodeloc.com/*
// @match https://nodeloc.com/*
// @run-at document-start
// @grant none
// @noframes
// ==/UserScript==
(function () {
'use strict';
/* ======================= 可调参数 ======================= */
const CONFIG = {
// 刷新后默认是否收起右栏(true = 打开页面就收起;之后以下拉按钮的选择为准,记在 localStorage)
defaultCollapsed: true,
// 正文铺开后的最大宽度。'1200px' 之类为固定上限,居中显示;
// 想完全铺满屏幕可改成 'none',想更窄可改成 '1080px'。
mainMaxWidth: '1200px',
// 是否显示右下角的「收起/展开右栏」悬浮按钮(false = 不显示,仅靠 Alt+S 切换)
showButton: true,
};
/* ======================================================== */
const CLASS = 'nl-aside-collapsed';
const STORAGE_KEY = 'nodeloc-collapse-aside';
const BTN_ID = 'nl-aside-toggle';
const readSaved = () => {
try {
const v = localStorage.getItem(STORAGE_KEY);
return v === null ? CONFIG.defaultCollapsed : v === '1';
} catch (e) {
return CONFIG.defaultCollapsed;
}
};
const writeSaved = (v) => {
try { localStorage.setItem(STORAGE_KEY, v ? '1' : '0'); } catch (e) { /* 隐私模式下忽略 */ }
};
let collapsed = readSaved();
/* ---------- 样式 ---------- */
const CSS = `
/* 当 html 带 .${CLASS} 时生效。变量 --nl-main-max 控制正文最大宽度。 */
html.${CLASS} { --nl-main-max: ${CONFIG.mainMaxWidth}; }
/* 1) 隐藏右侧信息栏:
.cols > .aside 首页/节点页/帖子页/用户页等的右栏
.split > .split-aside 我的节点/收藏/邀请/关于等页面的右栏
.ob-page > .ob-aside 新人指引的右栏 */
html.${CLASS} :is(.cols > .aside, .split > .split-aside, .ob-page > .ob-aside) {
display: none !important;
}
/* 2) 原本的两栏 grid(.cols,含帖子页的 .cols.reading)压成单栏 */
html.${CLASS} .cols {
grid-template-columns: minmax(0, 1fr) !important;
justify-content: stretch !important;
}
/* 2b) 列表视图下站点会把 .cols[data-wide] 改成 flex 横向排,这里改回单栏 grid */
html.${CLASS} :is(.page, .page > .node-page) > .cols[data-wide] {
display: grid !important;
}
/* 3) 正文:铺开 + 居中。max-width 由 --nl-main-max 决定,两侧自动留白 */
html.${CLASS} .cols > .main {
width: 100% !important;
flex: none !important;
max-width: var(--nl-main-max, 1200px) !important;
margin-inline: auto !important;
}
/* 3b) 解除外层 1124px 的限宽,把右栏让出来的空间真正给到正文 */
html.${CLASS} .page > :where(.cols, .node-page) {
max-width: none;
}
/* 4) .split 版式的页面(我的节点 / 收藏 / 能量明细 / 邀请 / 关于 / 常见问题…) */
html.${CLASS} .split {
justify-content: center !important;
}
html.${CLASS} .split > .split-main {
flex: 1 1 100% !important;
width: 100% !important;
max-width: var(--nl-main-max, 1200px) !important;
margin-inline: auto !important;
}
/* 说明/条款一类的文档页保持窄栏,长文别拉太宽 */
html.${CLASS} .doc-page .split-main {
max-width: 860px !important;
}
/* 5) 新人指引 .ob-page */
html.${CLASS} .ob-page {
max-width: none;
}
html.${CLASS} .ob-page > .ob-main {
width: 100% !important;
max-width: var(--nl-main-max, 1200px) !important;
margin-inline: auto !important;
}
/* ---------- 右下角悬浮按钮 ---------- */
#${BTN_ID} {
position: fixed;
z-index: 2147483000;
right: 20px;
bottom: 24px;
display: inline-flex;
align-items: center;
gap: 6px;
height: 40px;
padding: 0 14px;
border: 1px solid var(--line, #e3e6e4);
border-radius: 999px;
background: var(--panel, var(--surface, #fff));
color: var(--ink, #0f1a1c);
font: 600 13px/1 "IBM Plex Sans", "Noto Sans SC", system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
cursor: pointer;
opacity: 0.92;
box-shadow: var(--shadow-pop, 0 6px 20px rgba(15, 26, 28, .18));
transition: opacity .15s ease, transform .15s ease;
-webkit-tap-highlight-color: transparent;
user-select: none;
}
#${BTN_ID}:hover { opacity: 1; }
#${BTN_ID}:active { transform: scale(.96); }
#${BTN_ID} svg { width: 16px; height: 16px; flex: none; }
/* 手机上站点右下角有发帖按钮,往上让一让 */
@media (max-width: 860px) {
#${BTN_ID} { right: 14px; bottom: 88px; }
}
@media print {
#${BTN_ID} { display: none !important; }
}
`;
function injectStyle() {
if (document.getElementById('nl-aside-style')) return;
const parent = document.head || document.documentElement;
if (!parent) return;
const style = document.createElement('style');
style.id = 'nl-aside-style';
style.textContent = CSS;
parent.appendChild(style);
}
/* ---------- 状态应用 ---------- */
function applyState() {
if (!document.documentElement) return;
document.documentElement.classList.toggle(CLASS, collapsed);
const btn = document.getElementById(BTN_ID);
if (btn) {
btn.setAttribute('aria-pressed', String(collapsed));
btn.title = (collapsed ? '展开' : '收起') + '右侧信息栏(Alt+S)';
const label = btn.querySelector('.nl-label');
if (label) label.textContent = collapsed ? '展开右栏' : '收起右栏';
}
}
function toggle() {
collapsed = !collapsed;
writeSaved(collapsed);
applyState();
}
/* ---------- 悬浮按钮 ---------- */
function mountButton() {
if (!CONFIG.showButton) return;
if (!document.body || document.getElementById(BTN_ID)) return;
const btn = document.createElement('button');
btn.type = 'button';
btn.id = BTN_ID;
btn.innerHTML =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" ' +
'stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
'<rect x="3" y="4" width="18" height="16" rx="2"></rect><path d="M15 4v16"></path></svg>' +
'<span class="nl-label"></span>';
btn.addEventListener('click', toggle);
document.body.appendChild(btn);
applyState();
}
/* ---------- 启动 ---------- */
function init() {
injectStyle();
applyState();
}
if (document.documentElement) {
init();
} else {
document.addEventListener('DOMContentLoaded', init, { once: true });
}
if (document.body) {
mountButton();
} else {
document.addEventListener('DOMContentLoaded', mountButton, { once: true });
}
// 快捷键:Alt+S 切换
document.addEventListener('keydown', function (e) {
if (e.altKey && !e.ctrlKey && !e.metaKey && (e.code === 'KeyS' || (e.key || '').toLowerCase() === 's')) {
e.preventDefault();
toggle();
}
});
// 来回切换页面(含站点内部的局部刷新)时保持状态
window.addEventListener('pageshow', applyState);
})();
0
2
2 条回复
排序