// ==UserScript== // @name MSN Enhanced Pro // @namespace https://docs.scriptcat.org/ // @version 1.9 // @description MSN网站天气页面、日历页面及新闻页面清理及净化 // @author ZZW // @match https://*.msn.cn/* // @grant GM_addStyle // @run-at document-idle // ==/UserScript== (function () { 'use strict'; // 当前页面URL const currentUrl = location.href; const url = new URL(currentUrl); // 判断是否为天气页面 const isWeatherPage = currentUrl.includes('/zh-cn/weather/'); // 判断是否为天气页面子路径 const isForecastPage = currentUrl.includes('/zh-cn/weather/forecast/'); const isWeatherMapsPage = url.pathname.startsWith('/zh-cn/weather/maps'); const isHourlyForecastPage = currentUrl.includes('/zh-cn/weather/hourlyforecast/'); const isMonthlyForecastPage = currentUrl.includes('/zh-cn/weather/monthlyforecast/'); const isRecordsPage = currentUrl.includes('/zh-cn/weather/records/'); // 判断是否为日历页面 const isCalendarPage = url.pathname.startsWith('/zh-cn/lifestyle/calendar'); // 判断是否为游戏页面 const isGamingPage = url.pathname.startsWith('/zh-cn/gaming'); // 判断是否为新闻页面 const isNewsPage = /^https:\/\/www\.msn\.cn\/zh-cn\/(news|gaming|money|技术)\/(other|.*)$/.test(currentUrl); // 判断是否为主页 const isHomePage = currentUrl === 'https://www.msn.cn/zh-cn/'; /* ==================== 样式注入模块 ==================== */ function injectStyles() { const styles = ` /* ============ 天气forecast页面样式 ============ */ ${isForecastPage ? ` #WeatherRightRail, div.weatherMobileUpsellRoot-DS-EntryPoint1-1 > div, div.feedsSection-DS-EntryPoint1-1, div.newsContent-DS-EntryPoint1-1, a.button-DS-EntryPoint1-1.feedback_link_default-DS-EntryPoint1-1, div.themeSettingContainer-DS-EntryPoint1-1, button.themeSettingButton-DS-EntryPoint1-1, #WeatherDetailsSun-ScreenWidthSunMoon-c4 { display: none !important; } #WeatherOverviewV2-ScreenWidth-c4, #WeatherOverviewV2-ScreenWidthSevereWeather-c4, div.backgroundContainerCompact-DS-EntryPoint1-1, div.overallContainer-DS-EntryPoint1-1 { min-width: 620px !important; width: 620px !important; } #WeatherMapMini-ScreenWidth-c4, #WeatherMapMini-ScreenWidthSevereWeather-c4, #WeatherMapMini-ScreenWidthSunMoon-c4 { min-width: 620px !important; width: 620px !important; position: absolute; right: 0; } div.tabDailyItem-DS-EntryPoint1-1, div:nth-child(n+1) > button.tabDailyButton-DS-EntryPoint1-1.dayCardV3.overview, div:nth-child(n+1) > button.tabDailyButton-DS-EntryPoint1-1.dayCardV3, div:nth-child(n+1) > button > div { min-width: 201px !important; width: 201px !important; } div.tabDailyItem-DS-EntryPoint1-1.dailyTabSelected, div.tabDailyItem-DS-EntryPoint1-1.dailyTabSelected > button.tabDailyButton-DS-EntryPoint1-1.dayCardV3.overview, div.tabDailyItem-DS-EntryPoint1-1.dailyTabSelected > button.tabDailyButton-DS-EntryPoint1-1.dayCardV3, div.tabDailyItem-DS-EntryPoint1-1.dailyTabSelected > button > div { min-width: 205px !important; width: 205px !important; } div[class^="detailsContainer-DS-EntryPoint1-"], div[class^="detialsTrendChartSection-DS-EntryPoint1-"], div[class^="trendChartContainer-DS-EntryPoint1-"] { min-width: 1256px !important; width: 1256px !important; margin: 0 auto !important; } div[class^="headerContainer-DS-EntryPoint1-"], div[class^="trendChartContent-DS-EntryPoint1-"], div[class^="legendContainer-DS-EntryPoint1-"] { min-width: 1224px !important; width: 1224px !important; margin-left: 0 !important; margin-right: auto !important; } div[class^="trendChartViewArea-DS-EntryPoint1-"], div[class^="trendChartSVGContainer-DS-EntryPoint1-"], div[class^="trendChartViewArea-DS-EntryPoint1-"][class$=".overview"] > div > svg { min-width: 1180px !important; width: 1180px !important; } li.monthTabItem-DS-EntryPoint1-1 { min-width: 97.66px !important; width: 97.66px !important; } div[class^="sectionTitle-DS-EntryPoint1-"] { min-width: 940px !important; width: 940px !important; } div[class^="sectionTitle-DS-EntryPoint1-"] { min-width: 72px !important; width: 72px !important; } div[class^="select-DS-EntryPoint1-"], button[id^="selectYearSelect"][class^="select_selectedOption-DS-EntryPoint1-"], button[id^="selectMonthSelect"][class^="select_selectedOption-DS-EntryPoint1-"] { min-width: 260px !important; width: 260px !important; } #selectYearSelect > div[class^="select_selectedOptionIcon-DS-EntryPoint1-"], #selectYearSelect > div[class^="select_selectedOptionChevron-DS-EntryPoint1-"], #selectMonthSelect > div[class^="select_selectedOptionIcon-DS-EntryPoint1-"], #selectMonthSelect > div[class^="select_selectedOptionChevron-DS-EntryPoint1-"], #selectYearSelect > div:nth-child(2), #selectMonthSelect > div:nth-child(2) { min-width: 80px !important; width: 80px !important; } div.weatherClimateTable-DS-EntryPoint1-1 { min-width: 620px !important; width: 620px !important; } #WeatherForecast-ScreenWidth-c4, div.weatherDailyForecastContainer-DS-EntryPoint1-1, div.tabsContainer-DS-EntryPoint1-1, div.tabsContent-DS-EntryPoint1-1, div.tabsCarousel-DS-EntryPoint1-1, div.tabsCarouselItems-DS-EntryPoint1-1 , div[class*="trendListContainer-DS-EntryPoint1-"], div[class*="trendListCarousel-DS-EntryPoint1-"], div[class*="trendListLegend-DS-EntryPoint1-"], #WeatherDetailsSection-ScreenWidth-c4, div.sectionTitleContainer-DS-PKpK26.sectionTitleW4-DS-KQnWjP, #WeatherMapMiniV2-ScreenWidth-c4, div.mapMiniV2_root-DS-EntryPoint1-1.msn-DS-EntryPoint1-1, div.mapMiniV2Titile-DS-EntryPoint1-1, div.mapMiniV2C-DS-EntryPoint1-1.msn_size-DS-EntryPoint1-1, #WeatherMonthlySection-ScreenWidth-c4, #WeatherMonthCalendar-ScreenWidth-c4, div.maplibregl-canvas-container.maplibregl-interactive, div.maplibregl-canvas-container.maplibregl-interactive > canvas, canvas.maplibregl-canvas.atlas-map-canvas.wx-gl-canvas-cursor-default, div.monthCalendarRoot-DS-EntryPoint1-1.visualReady, div[class^="monthCalendarTabsSection-DS-EntryPoint1-"], ul[id^="monthTabContainer-DS-EntryPoint1-"], div[class^="monthCalendarTableRoot-DS-EntryPoint1-"], div[class^="monthCalendarTableContainer-DS-EntryPoint1-"], #WeatherTrendSection-ScreenWidth-c4, div.weatherHistoricalTrends_root-DS-EntryPoint1-1 { width: 1260px !important; margin: 0 auto !important; } #WeatherDetailsSection-ScreenWidth-c4 { min-height: 872px !important; height: 872px !important; } #WeatherMapMiniV2-ScreenWidth-c4 { min-height: 600px !important; height: 600px !important; } #WeatherMonthlySection-ScreenWidth-c4 { min-height: 956px !important; height: 956px !important; } div[class^="mapMiniV2Titile-DS-EntryPoint1-"], div[class^="monthCalendarTitlePhase2-DS-EntryPoint1-"], div[class^="monthCalendarTitlePhase2Dark-DS-EntryPoint1-"] { min-height: 50px !important; height: 50px !important; } div[class^="monthCalendarTabsSection-DS-EntryPoint1-"] { min-height: 60px !important; height: 60px !important; } ` : ''} /* ============ 天气maps页面样式 ============ */ ${isWeatherMapsPage ? ` a.button-DS-EntryPoint1-1.feedback_link_default-DS-EntryPoint1-1, div.faqButton-DS-EntryPoint1-1.faqButtonPosition-DS-EntryPoint1-1 { display: none !important; } ` : ''} /* ============ 天气hourlyforecast页面样式 ============ */ ${isHourlyForecastPage ? ` #pageBlock_rightRail, div[class^="weatherMobileUpsellContainer-DS"], #pageBlock_news, a.button-DS-EntryPoint1-1.feedback_link_default-DS-EntryPoint1-1 { display: none !important; } #pageBlock_chart, #pageBlock_table { min-width: 1230px !important; width: 1230px !important; margin: 0 auto !important; } ` : ''} /* ============ 天气monthlyforecast页面样式 ============ */ ${isMonthlyForecastPage ? ` #WeatherRightRail, #WeatherRightRail-ScreenWidth-c4h, #WeatherRightRail-ScreenWidth-c4, div.weatherMobileUpsellContainer-DS-EntryPoint1-1, div.feedsSection-DS-EntryPoint1-1, div.newsContent-DS-EntryPoint1-1, a.button-DS-EntryPoint1-1.feedback_link_default-DS-EntryPoint1-1 { display: none !important; } #WeatherMonthCalendar-ScreenWidth-c4h, #WeatherMonthCalendar-ScreenWidth-c4, #WeatherMonthCalendar, #WeatherMonthCalendarSection, div.monthCalendarRoot-DS-EntryPoint1-1.visualReady, div.monthCalendarTabsSection-DS-EntryPoint1-1, div.monthTabRoot-DS-EntryPoint1-1, div.monthCalendarContent-DS-EntryPoint1-1, div.monthCalendarTableRoot-DS-EntryPoint1-1, div.monthCalendarTableContainer-DS-EntryPoint1-1 { min-width: 1236px !important; width: 1236px !important; margin: 0 auto !important; } #CalendarTabContainerId > li.monthTabItem-DS-EntryPoint1-1 { min-width: 95.66px !important; width: 95.66px !important; } div.monthCalendarTop-DS-EntryPoint1-1 { min-height: 30px !important; height: 30px !important; } div.monthCalendarTabsSection-DS-EntryPoint1-1 { min-height: 54px !important; height: 54px !important; } #root > div > div > main.content-DS-EntryPoint1-1 { max-height: 820px !important; min-height: 820px !important; height: 820px !important; } ` : ''} /* ============ 天气records页面样式 ============ */ ${isRecordsPage ? ` #WeatherRightRail-ScreenWidth-c4, #WeatherRightRail, div.feedsSection-DS-EntryPoint1-1, div.newsContent-DS-EntryPoint1-1, a.button-DS-EntryPoint1-1.feedback_link_default-DS-EntryPoint1-1 { display: none !important; } #WeatherHistoricalTrends-ScreenWidth-c4, #WeatherHistoricalTrends, #WeatherHistoricalTrends > div.weatherHistoricalTrends_root-DS-EntryPoint1-1, #WeatherHistoricalTrends > div > div.weatherHistoricalTrends_location-DS-EntryPoint1-1, #WeatherHistoricalTrends > div > header.weatherHistoricalTrends_top-DS-EntryPoint1-1, #WeatherHistoricalTrends > div > div.weatherHistoricalTrends_wrapper-DS-EntryPoint1-1, #WeatherTrendToday-ScreenWidth-c4, #WeatherTrendToday > div > div.weatherTrendTodayContainer-DS-EntryPoint1-1 { min-width: 1236px !important; width: 1236px !important; margin: 0 auto !important; } #WeatherHistoricalTrends > div > div.weatherHistoricalTrends_location-DS-EntryPoint1-1 { min-height: 28px !important; height: 28px !important; } #WeatherHistoricalTrends > div > header.weatherHistoricalTrends_top-DS-EntryPoint1-1 { min-height: 40px !important; height: 40px !important; } ` : ''} /* ============ 日历页面样式 ============ */ ${isCalendarPage ? ` /* 日历Feed区域隐藏样式 */ rich-calendar-landing-page::part(calendar-feeds-container-slot), #root > div > div > div:nth-child(4) > a, a.button-DS-EntryPoint1-1.feedback_link_default-DS-EntryPoint1-1 { display: none !important; } ` : ''} /* ============ 游戏页面样式 ============ */ ${isGamingPage ? ` /* 在此处添加需要的样式 */ ` : ''} `; GM_addStyle(styles); } /* ==================== 功能模块 ==================== */ // 主页元素清理功能(支持Shadow DOM) function initHomePageCleanup() { if (!isHomePage) return; // 目标元素选择器 const targetSelectors = [ '#entry-point-hp-wc-root-wrapper > div.content-container.fifth-column.ntp-feed > div.me-stripe-placeholder.compact', '#entry-point-hp-wc-root-wrapper > div.content-container.fifth-column.ntp-feed > div.me-stripe-placeholder.compact > div', 'div > msft-horizontal-card-slider' ]; // 递归查找Shadow DOM并移除元素 function removeFromShadowDOM(root) { // 检查当前根节点中是否有目标元素 targetSelectors.forEach(selector => { const elements = root.querySelectorAll(selector); elements.forEach(el => el.remove()); }); // 检查所有子节点,看是否有Shadow DOM const allNodes = root.querySelectorAll('*'); allNodes.forEach(node => { if (node.shadowRoot) { // 如果有Shadow DOM,递归处理 removeFromShadowDOM(node.shadowRoot); } }); } // 初始处理 function removeElements() { // 先处理主文档 removeFromShadowDOM(document); // 特别处理ENTRY-POINT宿主元素 const entryPoints = document.querySelectorAll('entry-point'); entryPoints.forEach(entryPoint => { if (entryPoint.shadowRoot) { removeFromShadowDOM(entryPoint.shadowRoot); } }); // 特别处理ME-STRIPE宿主元素 const meStripes = document.querySelectorAll('me-stripe'); meStripes.forEach(meStripe => { if (meStripe.shadowRoot) { removeFromShadowDOM(meStripe.shadowRoot); } }); } // 立即执行一次 removeElements(); // 轮询处理动态加载的元素 let attempts = 0; const maxAttempts = 50; // 增加尝试次数 const timer = setInterval(() => { if (++attempts > maxAttempts) { clearInterval(timer); return; } removeElements(); }, 800); // 缩短间隔时间 // 监听DOM变化,处理新增的Shadow DOM const observer = new MutationObserver((mutations) => { mutations.forEach(mutation => { mutation.addedNodes.forEach(node => { if (node.nodeType === 1) { // 元素节点 if (node.shadowRoot) { removeFromShadowDOM(node.shadowRoot); } // 检查子节点是否有Shadow DOM const childNodes = node.querySelectorAll('*'); childNodes.forEach(child => { if (child.shadowRoot) { removeFromShadowDOM(child.shadowRoot); } }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true, attributes: false }); } // 日历页面移除Feed功能 function initCalendarFeedRemoval() { if (!isCalendarPage) return; // 目标选择器 const HOST_SELECTOR = 'rich-calendar-landing-page'; const TARGET_SELECTOR = 'div[slot="calendar-feeds-container-slot"].grid-view-feed'; function removeFeed() { const host = document.querySelector(HOST_SELECTOR); if (!host || !host.shadowRoot) return false; const target = host.shadowRoot.querySelector(TARGET_SELECTOR); if (target) { target.remove(); return true; } return false; } // 轮询尝试 let attempts = 0; const timer = setInterval(() => { if (removeFeed() || ++attempts > 30) { clearInterval(timer); } }, 1000); // 页面可见时再尝试一次 document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') { removeFeed(); } }); } // 游戏页面清理shadow DOM元素 function initGamingShadowCleanup() { if (!isGamingPage) return; const hostSelector = 'gaming-page'; const targetSelectors = [ 'div.game-top-container', 'gaming-discover-strip', 'casual-games-zh-carousel' ]; function cleanShadowElements() { // 尝试获取宿主元素 const host = document.querySelector(hostSelector) || document.querySelector(`div > ${hostSelector}`) || document.querySelector(`main > ${hostSelector}`); if (!host || !host.shadowRoot) return false; let isCleaned = false; targetSelectors.forEach(selector => { const elements = host.shadowRoot.querySelectorAll(selector); if (elements.length) { elements.forEach(el => el.remove()); isCleaned = true; } }); return isCleaned; } let attempts = 0; const maxAttempts = 60; // 最多尝试60次 const timer = setInterval(() => { if (cleanShadowElements() || ++attempts >= maxAttempts) { clearInterval(timer); } }, 1000); // 监听DOM变化,动态处理新增元素 const observer = new MutationObserver(() => { cleanShadowElements(); }); observer.observe(document.body, { childList: true, subtree: true, attributes: false }); // 页面可见时再次尝试 document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') { cleanShadowElements(); } }); } // 返回顶部按钮功能 function initBackToTop() { if (isWeatherPage || isCalendarPage) return; const backToTopBtn = document.createElement('div'); backToTopBtn.id = 'backToTopBtn'; backToTopBtn.title = '返回顶部'; // 动态添加样式 Object.assign(backToTopBtn.style, { position: 'fixed', bottom: '50px', right: '25px', width: '70.6px', height: '70.6px', backgroundColor: '#000', borderRadius: '10px', cursor: 'pointer', zIndex: '9999', display: 'flex', alignItems: 'center', justifyContent: 'center', opacity: '0.7', transition: 'opacity 0.3s', boxShadow: '0 2px 10px rgba(0,0,0,0.3)' }); // 添加箭头符号 const arrow = document.createTextNode('⬆'); backToTopBtn.appendChild(arrow); Object.assign(arrow.parentElement.style, { color: 'white', fontSize: '30px', fontWeight: '900' }); // 添加悬停效果 backToTopBtn.addEventListener('mouseover', () => { backToTopBtn.style.opacity = '1'; backToTopBtn.style.transform = 'translateY(-3px)'; }); backToTopBtn.addEventListener('mouseout', () => { backToTopBtn.style.opacity = '0.7'; backToTopBtn.style.transform = 'translateY(0)'; }); document.body.appendChild(backToTopBtn); // 点击事件 backToTopBtn.addEventListener('click', () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }); // 初始隐藏 backToTopBtn.style.display = 'none'; // 滚动监听 window.addEventListener('scroll', () => { backToTopBtn.style.display = window.scrollY > 200 ? 'flex' : 'none'; }); } // 新闻页面清理功能 function initNewsCleanup() { if (!isNewsPage) return; // 注入新闻页面专属样式 const newsStyles = ` div.consumption-page-gridarea_content, div.consumption-page-gridarea_content > div.viewsHeaderWrapper, div.consumption-page-gridarea_content > div.consumption-page-content-wrapper.article_content, div.consumption-page-gridarea_content > div.consumption-page-eocb { min-width: 1100px !important; width: 1100px !important; } div.consumption-page-eocb { min-height: 60px !important; height: 60px !important; } `; GM_addStyle(newsStyles); function cleanUpNews() { const newsContainerSelector = 'div#consumption-feed-content, div.consumption-feed-content'; const allNewsContainers = document.querySelectorAll(newsContainerSelector); if (allNewsContainers.length <= 1) return; Array.from(allNewsContainers).slice(1).forEach(container => container.remove()); } function removeElements() { // 要移除的目标元素选择器 const selectors = [ 'div.consumption-page-gridarea_rail', 'div.consumption-page-gridarea_rail > div', 'div.consumption-page-feed-wrapper', '.consumption-page-feed-wrapper > :is(h1, h2, h3, h4, h5, h6)', '.recommended-sites-container', 'ul.recommendedSitesList.recommendedSitesListAutoGrow', 'div.views-right-rail-vertical', 'div:nth-child(N) > above-river-block', 'div > desktop-feed-views', 'div > views-native-ad.fetched' ]; selectors.forEach(selector => { const elements = document.querySelectorAll(selector); elements.forEach(el => { // 彻底从DOM中移除元素 el.remove(); }); }); } // 页面加载后执行 window.addEventListener('load', () => { cleanUpNews(); removeElements(); const observer = new MutationObserver((mutations) => { let needsCleanup = false; mutations.forEach(m => m.addedNodes.length > 0 && (needsCleanup = true)); needsCleanup && (cleanUpNews(), removeElements()); }); observer.observe(document.body, { childList: true, subtree: true }); }); } /* ==================== 初始化执行 ==================== */ // 页面加载完成后注入样式 window.addEventListener('load', () => { injectStyles(); }); // 初始化主页清理功能 initHomePageCleanup(); // 初始化日历Feed移除功能 initCalendarFeedRemoval(); // 初始化游戏页面shadow DOM元素清理 initGamingShadowCleanup(); // 初始化返回顶部按钮 initBackToTop(); // 初始化新闻清理 initNewsCleanup(); })();