// ==UserScript==
// @name 小草简洁助手
// @namespace http://tampermonkey.net/
// @version 2.20
// @description 添加上一帖和下一帖的悬浮按钮,并根据提取的链接进行跳转,同时提供设置选项,包括上下帖导航、限制页面宽度为1080、等比例无缝看图和图片查看模式
// @match *://*/htm_data/*
// @match http*://*/htm_data/*.html
// @match http*://*/htm_mob/*.html
// @match http*://*/read.php*
// @match http*://*/personal.php*
// @match http*://*/post.php*
// @match http*://*/thread0806.php*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// 上下帖导航
var enableNavigationButton = GM_getValue('enableNavigationButton', true);
// 预加载下一帖
var enablePreloadnextpage = GM_getValue('enablePreloadnextpage', true);
// 限制页面宽度为1080
var limitPageWidth = GM_getValue('limitPageWidth', true);
// 等比例无缝看图
var enableSeamlessView = GM_getValue('enableSeamlessView', true);
// 图片查看模式
var enableImagePreview = GM_getValue('enableImagePreview', true);
// 使用手机版面
var enableMobilePage = GM_getValue('enableMobilePage',true)
// 手机版帖子简洁
var enableClearPage = GM_getValue('enableClearPage',true)
GM_registerMenuCommand('小草简洁助手 设置', function() {
var settingsWindow = document.getElementById('settingsWindow');
if (settingsWindow) {
settingsWindow.style.display = 'block';
} else {
createSettingsWindow();
}
});
function createSettingsWindow() {
var settingsWindow = document.createElement('div');
settingsWindow.id = 'settingsWindow';
settingsWindow.style.position = 'fixed';
settingsWindow.style.top = '50%';
settingsWindow.style.left = '50%';
settingsWindow.style.transform = 'translate(-50%, -50%)';
settingsWindow.style.width = '200px';
settingsWindow.style.height = '280px';
settingsWindow.style.backgroundColor = '#fff';
settingsWindow.style.border = '1px solid #ccc';
settingsWindow.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.2)';
settingsWindow.style.padding = '20px';
settingsWindow.style.zIndex = '9999';
var titleLabel = document.createElement('h3');
titleLabel.innerHTML = '小草简洁助手 设置';
settingsWindow.appendChild(titleLabel);
var enableNavigationButtonCheckbox = createCheckbox('enableNavigationButton', '上下帖导航', enableNavigationButton);
settingsWindow.appendChild(enableNavigationButtonCheckbox);
var preloadnextpageCheckbox = createCheckbox('enablePreloadnextpage', '预加载下一帖', enablePreloadnextpage);
settingsWindow.appendChild(preloadnextpageCheckbox);
var limitWidthCheckbox = createCheckbox('limitPageWidth', '限制页面宽度为1080', limitPageWidth);
settingsWindow.appendChild(limitWidthCheckbox);
var seamlessViewCheckbox = createCheckbox('enableSeamlessView', '等比例无缝看图', enableSeamlessView);
settingsWindow.appendChild(seamlessViewCheckbox);
var imagePreviewCheckbox = createCheckbox('enableImagePreview', '图片查看模式', enableImagePreview);
settingsWindow.appendChild(imagePreviewCheckbox);
var MobilePageCheckbox = createCheckbox('enableMobilePage', '使用手机版面', enableMobilePage);
settingsWindow.appendChild(MobilePageCheckbox);
var ClearPageCheckbox = createCheckbox('enableClearPage', '手机版帖子简洁', enableClearPage);
settingsWindow.appendChild(ClearPageCheckbox);
var saveButton = document.createElement('button');
saveButton.innerHTML = '保存';
saveButton.addEventListener('click', function() {
enableNavigationButton = document.getElementById('enableNavigationButton').checked;
GM_setValue('enableNavigationButton', enableNavigationButton);
enablePreloadnextpage = document.getElementById('enablePreloadnextpage').checked;
GM_setValue('enablePreloadnextpage', enablePreloadnextpage);
limitPageWidth = document.getElementById('limitPageWidth').checked;
GM_setValue('limitPageWidth', limitPageWidth);
enableSeamlessView = document.getElementById('enableSeamlessView').checked;
GM_setValue('enableSeamlessView', enableSeamlessView);
enableImagePreview = document.getElementById('enableImagePreview').checked;
GM_setValue('enableImagePreview', enableImagePreview);
enableMobilePage = document.getElementById('enableMobilePage').checked;
GM_setValue('enableMobilePage', enableMobilePage);
enableClearPage = document.getElementById('enableClearPage').checked;
GM_setValue('enableClearPage', enableClearPage);
var successLabel = document.getElementById('successLabel');
successLabel.style.display = 'block';
setTimeout(function() {
settingsWindow.style.display = 'none';
successLabel.style.display = 'none';
}, 2000);
});
settingsWindow.appendChild(saveButton);
var successLabel = document.createElement('label');
successLabel.id = 'successLabel';
successLabel.innerHTML = '保存成功,刷新页面生效!';
successLabel.style.display = 'none';
settingsWindow.appendChild(successLabel);
document.body.appendChild(settingsWindow);
}
function createCheckbox(id, label, checked) {
var checkbox = document.createElement('input');
checkbox.id = id;
checkbox.type = 'checkbox';
checkbox.checked = checked;
var checkboxLabel = document.createElement('label');
checkboxLabel.innerHTML = label;
checkboxLabel.setAttribute('for', id);
var container = document.createElement('div');
container.appendChild(checkbox);
container.appendChild(checkboxLabel);
return container;
}
if (window.location.href.includes('/thread0806.php?fid=')) {
// 存储提取的链接
var links = [];
// 获取所有主题链接
var threadLinks = document.querySelectorAll('a[href^="htm_mob"]');
// 提取链接并存储到数组
for (var i = 0; i < threadLinks.length; i++) {
links.push(threadLinks[i].href);
}
// 存储链接数组到本地存储
localStorage.setItem('threadLinks', JSON.stringify(links));
} else if (window.location.href.includes('/htm_mob/')) {
// 获取本地存储中的链接数组
links = JSON.parse(localStorage.getItem('threadLinks')) || [];
// 获取当前页面的 URL
var currentURL = window.location.href;
// 获取当前页面在链接数组中的索引
var currentIndex = links.indexOf(currentURL);
// 如果启用导航按钮并且当前页面在链接数组中,且不是最后一个链接
if (enableNavigationButton && currentIndex !== -1 && currentIndex < links.length - 1) {
var nextURL = links[currentIndex + 1];
// 获取下一帖的内容并提取图片链接
GM_xmlhttpRequest({
method: 'GET',
url: nextURL,
onload: function(response) {
var html = response.responseText;
var imageLinks = extractImageLinks(html);
// 创建图片容器并显示图片链接
if (enablePreloadnextpage) {
displayImageLinks(imageLinks);
}
}
});
}
}
if (enableNavigationButton && window.location.href.includes('/htm_mob/')) {
var previousButton = createPreviousButton();
var nextButton = createNextButton();
}
function createPreviousButton() {
var previousButton = document.createElement('button');
previousButton.innerHTML = '上一帖';
previousButton.style.position = 'fixed';
previousButton.style.left = '40%';
previousButton.style.height = '25px';
previousButton.style.backgroundColor = '#4CAF50';
previousButton.style.border = 'none';
previousButton.style.color = 'white';
previousButton.style.textAlign = 'center';
previousButton.style.textDecoration = 'none';
previousButton.style.fontSize = '14px';
previousButton.style.borderRadius = '4px';
previousButton.style.cursor = 'pointer';
previousButton.style.bottom = '10px';
previousButton.style.transform = 'translateX(50%)';
previousButton.style.zIndex = '9999';
previousButton.addEventListener('click', function() {
navigateToPreviousPost();
});
document.body.appendChild(previousButton);
return previousButton;
}
function createNextButton() {
var nextButton = document.createElement('button');
nextButton.innerHTML = '下一帖';
nextButton.style.position = 'fixed';
nextButton.style.height = '25px';
nextButton.style.backgroundColor = '#4CAF50';
nextButton.style.border = 'none';
nextButton.style.color = 'white';
nextButton.style.textAlign = 'center';
nextButton.style.textDecoration = 'none';
nextButton.style.fontSize = '14px';
nextButton.style.borderRadius = '4px';
nextButton.style.cursor = 'pointer';
nextButton.style.bottom = '10px';
nextButton.style.left = '60%';
nextButton.style.transform = 'translateX(50%)';
nextButton.style.zIndex = '9999';
nextButton.addEventListener('click', function() {
navigateToNextPost();
});
document.body.appendChild(nextButton);
return nextButton;
}
function navigateToPreviousPost() {
var currentURL = window.location.href;
var currentIndex = links.indexOf(currentURL);
if (currentIndex !== -1 && currentIndex > 0) {
var previousURL = links[currentIndex - 1];
window.location.href = previousURL;
}
}
function navigateToNextPost() {
var currentURL = window.location.href;
var currentIndex = links.indexOf(currentURL);
if (currentIndex !== -1 && currentIndex < links.length - 1) {
var nextURL = links[currentIndex + 1];
window.location.href = nextURL;
}
}
// 提取图片链接
function extractImageLinks(html) {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
var divContainer = doc.getElementById('conttpc');
var images = divContainer.querySelectorAll('img[ess-data]');
var imageLinks = [];
for (var i = 0; i < images.length; i++) {
var image = images[i];
var imageLink = image.getAttribute('ess-data');
imageLinks.push(imageLink);
}
return imageLinks;
}
// 创建并显示图片容器
function displayImageLinks(imageLinks) {
// 创建图片容器
var imageContainer = document.createElement('div');
imageContainer.className = 'image-container';
imageContainer.style.width = '90%';
imageContainer.style.height = '50px';
imageContainer.style.position = 'fixed';
imageContainer.style.bottom = '35px';
imageContainer.style.left = '50%';
imageContainer.style.transform = 'translateX(-50%)';
imageContainer.style.backgroundColor = 'rgba(0, 0, 0, 0.6)';
imageContainer.style.overflowX = 'scroll';
imageContainer.style.whiteSpace = 'nowrap';
imageContainer.style.zIndex = '9999';
imageContainer.style.opacity = '0';
imageContainer.style.transition = 'opacity 0.5s';
// 添加隐藏滚动条的样式
// imageContainer.style.overflow = 'hidden';
imageContainer.style.msOverflowStyle = 'none';
imageContainer.style.scrollbarWidth = 'none';
// 针对 Chrome 浏览器的滚动条隐藏样式
imageContainer.style.webkitOverflowScrolling = 'touch';
imageContainer.style.webkitOverflowScrolling = 'auto';
imageContainer.style.webkitOverflowScrolling = 'auto';
imageContainer.style.webkitScrollbar = 'none';
imageContainer.style.msOverflowStyle = 'none';
// 针对 Chrome 浏览器的滚动条隐藏样式
imageContainer.style.webkitScrollbar = 'none';
imageContainer.style.webkitScrollbarWidth = 'none';
imageContainer.style.webkitScrollbarColor = 'transparent';
// 添加图片到容器中
for (var i = 0; i < imageLinks.length; i++) {
var image = document.createElement('img');
image.src = imageLinks[i];
image.style.height = '47px';
image.style.width = 'auto';
image.style.display = 'inline-block';
image.style.margin = '0 1x';
imageContainer.appendChild(image);
}
// 监听图片加载完成事件
var images = imageContainer.querySelectorAll('img');
var loadedCount = 0;
for (var j = 0; j < images.length; j++) {
images[j].addEventListener('load', function() {
loadedCount++;
if (loadedCount === images.length) {
// 所有图片加载完成后设置容器透明度为60%
imageContainer.style.opacity = '0.2';
// 将下一帖按钮的标题改为"下一帖,预加载成功"
nextButton.innerHTML = '下一帖,预加载成功';
nextButton.style.left = '50%';
}
});
}
// 添加容器到页面
document.body.appendChild(imageContainer);
}
// 限制页面宽度为1080
if (limitPageWidth) {
var browserWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var targetWidth = Math.min(browserWidth, 1080);
document.body.style.maxWidth = targetWidth + 'px';
document.body.style.margin = '0 auto';
}
// 添加使用手机版页面功能的代码
if (enableMobilePage) {
var url = window.location.href;
var regex = /\/htm_data\//;
if (regex.test(url)) {
window.location.href = url.replace(regex, '/htm_mob/');
}
}
//帖子页面简洁
if(enableClearPage){
if (window.location.href.includes('/htm_mob/') || window.location.href.includes('read.php')) {
// 延迟1秒后执行以下代码
setTimeout(function() {
// 定义需要过滤的元素选择器
const adSelector = '.ad, .ads, .advertising, .advertisement, .ad-banner, .ad-container, .ad-frame, .ad-leaderboard, .ad-slot, .ad-wrapper, .banner-ad, .google-ads, .sponsored';
// 获取所有需要过滤的元素
const ads = document.querySelectorAll(adSelector);
// 遍历所有需要过滤的元素,并将其从DOM树中移除
ads.forEach(ad => {
ad.remove();
});
// 定义需要过滤的指定元素选择器
//const targetSelector = '.tpc_face, .tpc_face_svg,.tpc_icon,.f_one,.tpc_rp_btn,.fr,.post_comm,span.f18,div.t,.t_like,.h.guide,div.line:nth-child(3)';
const targetSelector = '.tpc_face,.tpc_icon.fl,.post_comm_face,.post_comm_face_svg,.f_one,.tpc_rp_btn,.fr,div.t,.t_like,.h.guide,div.line:nth-child(3)';
// 获取所有需要过滤的指定元素
const targets = document.querySelectorAll(targetSelector);
// 遍历所有需要过滤的指定元素,并将其从DOM树中移除
targets.forEach(target => {
target.remove();
});
// 处理用户名和时间为单行
var mainDiv = document.getElementById('main');
if (mainDiv) {
var tpcDivs = mainDiv.getElementsByClassName('tpc_detail f10 fl');
for (var i = 0; i < tpcDivs.length; i++) {
var div = tpcDivs[i];
var brTags = div.getElementsByTagName('br');
for (var j = brTags.length - 1; j >= 0; j--) {
var br = brTags[j];
br.parentNode.replaceChild(document.createTextNode(' '), br);
}
}
}
}, 1000); // 延迟1秒后执行以上代码
}
}
// 等比例无缝看图
if (enableSeamlessView) {
// 延迟执行函数
function delayedExecution() {
// 获取tpc_cont元素
var tpcCont = document.querySelector('div.tpc_cont');
if (tpcCont) {
// 获取tpc_cont元素下的所有子节点
var childNodes = tpcCont.childNodes;
// 遍历子节点
for (var i = childNodes.length - 1; i >= 0; i--) {
var node = childNodes[i];
// 如果节点是图片或视频元素
if (node.nodeType === Node.ELEMENT_NODE && (node.nodeName === 'IMG' || node.nodeName === 'VIDEO')) {
// 最大化尺寸
node.style.width = '100%';
node.style.height = 'auto';
}
// 判断节点名称是否为br
if (node.nodeType === Node.ELEMENT_NODE && node.nodeName === 'BR') {
var nextNode = node.nextSibling;
// 判断br标签下的内容是否为文本节点
if (nextNode && nextNode.nodeType === Node.TEXT_NODE) {
// 如果br标签下的内容是文本节点,则保留br标签
continue;
} else {
// 删除br标签
tpcCont.removeChild(node);
continue;
}
}
}
// 删除整个div下的所有
tpcCont.innerHTML = tpcCont.innerHTML.replace(/ /g, '');
}
}
// 延迟3秒后执行函数
setTimeout(delayedExecution, 2000);
}
// 图片查看模式
if (enableImagePreview) {
function ImagePreview() {
// 获取当前浏览器的宽度
var browserWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
// 定义要限制的图片的目标宽度(最大为1080px)
var targetWidth = Math.min(browserWidth, 1080);
// 获取所有图片元素
var imgs = document.querySelectorAll('.tpc_cont img');
// 遍历所有图片元素
for (var i = 0; i < imgs.length; i++) {
var img = imgs[i];
// 添加点击事件,点击图片时进入查看模式
img.addEventListener('click', function (e) {
// 删除原图片的链接
this.removeAttribute('href');
// 阻止事件冒泡
e.stopPropagation();
// 获取该div下的所有图片元素
var div = this.parentNode;
var imgs = div.querySelectorAll('img');
// 创建一个div用于显示所有图片
var container = document.createElement('div');
container.style.position = 'fixed';
container.style.top = '0';
container.style.left = '0';
container.style.width = '100%';
container.style.height = '100%';
container.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
container.style.zIndex = '9999';
container.style.overflow = 'auto';
// 遍历所有图片元素,将它们拼接到一起
var imgContainer = document.createElement('div');
imgContainer.style.width = targetWidth + 'px'; // 设置图片容器宽度与目标宽度一致
imgContainer.style.margin = '0 auto'; // 设置图片容器居中
for (var i = 0; i < imgs.length; i++) {
var img = imgs[i];
var src = img.getAttribute('src');
var imgEl = document.createElement('img');
imgEl.setAttribute('src', src);
imgEl.style.width = '100%'; // 设置图片宽度为100%(等比例缩放)
imgEl.style.height = 'auto'; // 设置图片高度为auto(根据宽度等比例缩放)
imgEl.style.display = 'block'; // 修改样式,使图片单列显示
imgContainer.appendChild(imgEl);
}
container.appendChild(imgContainer);
// 添加关闭按钮
var closeBtn = document.createElement('div');
closeBtn.style.position = 'absolute';
closeBtn.style.top = '10px';
closeBtn.style.right = '10px';
closeBtn.style.width = '30px';
closeBtn.style.height = '30px';
closeBtn.style.lineHeight = '30px';
closeBtn.style.textAlign = 'center';
closeBtn.style.backgroundColor = '#fff';
closeBtn.style.borderRadius = '50%';
closeBtn.style.cursor = 'pointer';
closeBtn.style.fontSize = '20px';
closeBtn.style.color = '#000';
closeBtn.innerHTML = '×';
closeBtn.addEventListener('click', function () {
container.parentNode.removeChild(container);
});
container.appendChild(closeBtn);
// 添加点击事件,点击拼接后的图片时关闭
imgContainer.addEventListener('click', function (e) {
// 阻止事件冒泡
e.stopPropagation();
// 关闭查看模式
container.parentNode.removeChild(container);
});
// 将div添加到页面中
document.body.appendChild(container);
}, true);
// 计算图片的高度,按比例进行放大
// var ratio = targetWidth / img.naturalWidth;
// var height = Math.round(img.naturalHeight * ratio);
// 设置图片的宽度和高度
// img.style.width = targetWidth + 'px';
// img.style.height = 'auto';
}
}setTimeout(ImagePreview, 2500);
}
})();