Skip to content

Instantly share code, notes, and snippets.

@Underquant
Last active April 24, 2026 05:28
Show Gist options
  • Select an option

  • Save Underquant/e8490a0e15542b3468e3694be16dab81 to your computer and use it in GitHub Desktop.

Select an option

Save Underquant/e8490a0e15542b3468e3694be16dab81 to your computer and use it in GitHub Desktop.
Weibo网页去广告/Weibo Adblock,移除微博推荐的未关注者(广告),并且动态调整页面布局。
// ==UserScript==
// @name Weibo网页去广告
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add a button to copy Weibo title, time, URL, and content without emoji
// @author You
// @match http://weibo.com/*
// @match https://weibo.com/*
// @grant none
// ==/UserScript==
(function() {
// 1. 创建控制面板
const panel = document.createElement('div');
panel.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 999999;
background: white;
padding: 15px;
border: 1px solid #ccc;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
display: flex;
flex-direction: column;
gap: 10px;
`;
const title = document.createElement('div');
title.innerText = '信息流无缝净化器';
title.style.fontWeight = 'bold';
title.style.textAlign = 'center';
const startBtn = document.createElement('button');
startBtn.innerText = '▶ 开启过滤与平移';
startBtn.style.cssText = 'padding: 8px 15px; background: #00a1d6; color: white; border: none; border-radius: 4px; cursor: pointer;';
const stopBtn = document.createElement('button');
stopBtn.innerText = '⏹ 停止';
stopBtn.style.cssText = 'padding: 8px 15px; background: #f5f5f5; color: #333; border: 1px solid #ccc; border-radius: 4px; cursor: pointer;';
panel.appendChild(title);
panel.appendChild(startBtn);
panel.appendChild(stopBtn);
document.body.appendChild(panel);
// 2. 核心变量
let isFiltering = false;
let filterTimer = null;
// 用于记录每一个被隐藏坑位(通过 Vue 的原始 translateY 识别)的高度
const hiddenHeightsMap = new Map();
// 解析 Vue 设定的原始 Y 坐标
const getVueY = (el) => {
const match = el.style.transform.match(/translateY\(([\d.-]+)px\)/);
return match ? parseFloat(match[1]) : 0;
};
// 3. 执行过滤与坐标平移
function processFilter() {
if (!isFiltering) return;
// 获取所有虚拟滚动的坑位
const views = Array.from(document.querySelectorAll('.vue-recycle-scroller__item-view'));
// 必须按照 Vue 赋予的 Y 坐标从上到下排序,保证累计偏移量计算正确
views.sort((a, b) => getVueY(a) - getVueY(b));
views.forEach(view => {
const originalY = getVueY(view);
const innerContainer = view.firstElementChild; // 真正装载微博内容的容器
if (!innerContainer) return;
// 寻找关注按钮
const followBtn = view.querySelector('header button');
let isRecommendation = false;
if (followBtn) {
const btnText = followBtn.innerText.trim();
// 判断是否为未关注推荐
if (btnText.includes('关注') && !btnText.includes('已') && !btnText.includes('互')) {
isRecommendation = true;
}
}
if (isRecommendation) {
// 如果是推荐:记录其高度,并使其视觉上消失
if (!hiddenHeightsMap.has(originalY)) {
const h = view.offsetHeight;
if (h > 0) hiddenHeightsMap.set(originalY, h);
}
// 不使用 display: none,而是用透明度和禁用点击,防止破坏 Vue 的测量机制
view.style.opacity = '0';
view.style.pointerEvents = 'none';
} else {
// 如果是正常微博:恢复视觉属性
view.style.opacity = '1';
view.style.pointerEvents = 'auto';
}
// --- 核心平移逻辑 ---
// 计算当前微博上方,所有被隐藏微博的累计高度
let cumulativeOffset = 0;
for (let [hiddenY, hiddenHeight] of hiddenHeightsMap.entries()) {
if (hiddenY < originalY) {
cumulativeOffset += hiddenHeight;
}
}
// 将内部容器向上平移,填补空白区域
innerContainer.style.transition = 'transform 0.3s ease'; // 添加一点平滑动画
innerContainer.style.transform = `translateY(-${cumulativeOffset}px)`;
});
}
// 4. 绑定按钮事件
startBtn.onclick = () => {
if (isFiltering) return;
isFiltering = true;
startBtn.style.background = '#ccc';
startBtn.innerText = '🛡️ 无缝净化中...';
// 开启高频检测 (200ms) 确保滚动时平移能够顺滑跟上
filterTimer = setInterval(processFilter, 200);
};
stopBtn.onclick = () => {
isFiltering = false;
clearInterval(filterTimer);
startBtn.style.background = '#00a1d6';
startBtn.innerText = '▶ 开启过滤与平移';
// 清理现场,恢复所有微博的位置和显示
const views = document.querySelectorAll('.vue-recycle-scroller__item-view');
views.forEach(view => {
view.style.opacity = '1';
view.style.pointerEvents = 'auto';
if (view.firstElementChild) {
view.firstElementChild.style.transform = 'translateY(0px)';
}
});
hiddenHeightsMap.clear();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment