// ==UserScript== // @name 简化新华网页面 // @namespace https://bbs.tampermonkey.net.cn/ // @version 1.1 // @description 去除新华网中的特定元素并添加回到顶部按钮 // @author ZZW // @match https://www.xinhuanet.com/* // @match https://www.news.cn/* // @match https://*.news.cn/* // @grant none // ==/UserScript== (function() { 'use strict'; /** * 移除指定选择器对应的元素 */ function removeElements() { const selectors = [ 'div.main-right.right', 'div.fix-ewm.domPC > img', 'div.main-bar' ]; selectors.forEach(selector => { const elements = document.querySelectorAll(selector); elements.forEach(element => { if (element) { element.parentNode.removeChild(element); } }); }); } /** * 修改指定选择器对应元素的宽度 */ function changeWidth() { const targetWidth = '1200px'; const originalWidthToKeep = '1000px'; const targetSelectors = [ 'div.main-left.left', '#detail' ]; targetSelectors.forEach(selector => { const elements = document.querySelectorAll(selector); elements.forEach(element => { if (element) { const currentWidth = parseInt(window.getComputedStyle(element).width, 10); const originalWidthValue = parseInt(originalWidthToKeep, 10); if (currentWidth < originalWidthValue) { element.style.width = targetWidth; } } }); }); } /** * 创建回到顶部按钮 */ function createBackToTopButton() { const targetElement = document.querySelector('span.fxd-khd'); if (!targetElement) return; // 如果目标元素不存在则不创建按钮 const backToTopBtn = document.createElement('button'); backToTopBtn.textContent = '▲'; backToTopBtn.id = 'back-to-top-btn'; const targetRect = targetElement.getBoundingClientRect(); // 样式设置 Object.assign(backToTopBtn.style, { width: '50px', height: '50px', position: 'fixed', left: `${targetRect.left + window.scrollX}px`, top: `${targetRect.bottom + window.scrollY + 30}px`, backgroundColor: '#000', color: 'white', border: 'none', borderRadius: '5px', cursor: 'pointer', fontSize: '24px', display: 'flex', alignItems: 'center', justifyContent: 'center', zIndex: '9999' }); backToTopBtn.addEventListener('click', () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }); document.body.appendChild(backToTopBtn); } removeElements(); changeWidth(); createBackToTopButton(); })();