// ==UserScript== // @name MSN Enhanced // @namespace https://docs.scriptcat.org/ // @version 2.4 // @description MSN网站天气页面、日历页面及新闻页面清理及净化 // @author ZZW // @match https://*.msn.cn/* // @require https://lib.baomitu.com/jquery/3.5.0/jquery.min.js // @grant GM_addStyle // @run-at document-idle // ==/UserScript== (function() { 'use strict'; /* global $ */ // ==================== 网站配置 ==================== const siteConfig = { // MSN 主页 msnHome: { match: /^https:\/\/www\.msn\.cn\/zh-cn\/?(?:\?.*)?$/, styles: '', // 主页无需额外样式 handler: initHomePageCleanup }, // 天气 - 预报页 msnWeatherForecast: { match: /\/zh-cn\/weather\/forecast\//, styles: ` #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: 705px !important; width: 705px !important; } div[class^="select-DS-EntryPoint1-"], button[id^="selectYearSelect"][class^="select_selectedOption-DS-EntryPoint1-1"], button[id^="selectMonthSelect"][class^="select_selectedOption-DS-EntryPoint1-1"] { min-width: 260px !important; width: 260px !important; } #selectYearSelect > div[class^="select_selectedOptionIcon-DS-EntryPoint1-1"], #selectYearSelect > div[class^="select_selectedOptionChevron-DS-EntryPoint1-1"], #selectMonthSelect > div[class^="select_selectedOptionIcon-DS-EntryPoint1-1"], #selectMonthSelect > div[class^="select_selectedOptionChevron-DS-EntryPoint1-1"], #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; } `, }, // 天气 - 地图页 msnWeatherMaps: { match: /\/zh-cn\/weather\/maps/, styles: ` a.button-DS-EntryPoint1-1.feedback_link_default-DS-EntryPoint1-1, div.faqButton-DS-EntryPoint1-1.faqButtonPosition-DS-EntryPoint1-1 { display: none !important; } `, }, // 天气 - 每小时预报页 msnWeatherHourly: { match: /\/zh-cn\/weather\/hourlyforecast\//, styles: ` #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; } `, }, // 天气 - 每月预报页 msnWeatherMonthly: { match: /\/zh-cn\/weather\/monthlyforecast\//, styles: ` #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; } `, }, // 天气 - 记录页 msnWeatherRecords: { match: /\/zh-cn\/weather\/records\//, styles: ` #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; } `, }, // 日历页面 msnCalendar: { match: (url) => new URL(url).pathname.startsWith('/zh-cn/lifestyle/calendar'), styles: ` 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; } `, handler: initCalendarFeedRemoval }, // 游戏页面 msnGaming: { match: (url) => new URL(url).pathname.startsWith('/zh-cn/gaming'), styles: '', // 暂无额外样式 handler: initGamingShadowCleanup }, // 新闻页面(排除游戏) msnNews: { match: /^https:\/\/www\.msn\.cn\/zh-cn\/(news|money|%E6%8A%80%E6%9C%AF)\/(other|.*)$/, styles: ` 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; } `, handler: initNewsCleanup } }; // ==================== 功能模块 ==================== // 主页元素清理(处理 Shadow DOM) function initHomePageCleanup() { const targetSelectors = [ 'entry-point::shadow #entry-point-hp-wc-root-wrapper > div.content-container.fifth-column.ntp-feed > div.me-stripe-placeholder.compact', 'entry-point::shadow #entry-point-hp-wc-root-wrapper > div.content-container.fifth-column.ntp-feed > div.me-stripe-placeholder.compact > div', 'entry-point::shadow #entry-point-hp-wc-root-wrapper > div.content-container.fifth-column.ntp-feed > div.me-stripe-placeholder.compact > div.me-stripe-container > me-stripe::shadow msft-horizontal-card-slider' ]; function queryShadowDom(element, selectors) { if (selectors.length === 0) return [element]; const currentSelector = selectors[0]; let results = []; const nodes = element instanceof ShadowRoot ? element.querySelectorAll(currentSelector) : element.shadowRoot ? element.shadowRoot.querySelectorAll(currentSelector) : element.querySelectorAll(currentSelector); if (selectors.length > 1) { nodes.forEach(node => { const subResults = queryShadowDom(node, selectors.slice(1)); results = [...results, ...subResults]; }); } else { results = Array.from(nodes); } return results; } function cleanElements() { targetSelectors.forEach(selector => { const parts = selector.split('::shadow '); const hostSelector = parts[0]; const shadowPaths = parts.slice(1); const host = document.querySelector(hostSelector); if (!host) return; const elements = queryShadowDom(host, shadowPaths); if (elements.length) { elements.forEach(el => el.remove()); console.log(`已移除多层Shadow DOM元素:${selector}`); } }); } cleanElements(); let attempts = 0; const maxAttempts = 100; const pollTimer = setInterval(() => { cleanElements(); if (++attempts >= maxAttempts) { clearInterval(pollTimer); console.log("多层Shadow DOM清理轮询结束"); } }, 300); const observer = new MutationObserver((mutations) => { mutations.forEach(mutation => { if (mutation.addedNodes.length > 0) { cleanElements(); } }); }); observer.observe(document, { childList: true, subtree: true, attributes: false }); document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') { cleanElements(); } }); } // 日历页面移除 Feed function initCalendarFeedRemoval() { 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() { 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; const timer = setInterval(() => { if (cleanShadowElements() || ++attempts >= maxAttempts) { clearInterval(timer); } }, 1000); const observer = new MutationObserver(() => { cleanShadowElements(); }); observer.observe(document.body, { childList: true, subtree: true, attributes: false }); document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') { cleanShadowElements(); } }); } // 新闻页面清理 function initNewsCleanup() { // 清理多余新闻容器 function cleanUpNews() { const newsContainerSelector = '#consumption-feed-content, ' + '.consumption-page-feed-content, ' + '.consumption-feed-content'; const allNewsContainers = document.querySelectorAll(newsContainerSelector); if (allNewsContainers.length <= 1) { console.log("仅检测到1个新闻容器,无需清理"); return; } Array.from(allNewsContainers).slice(1).forEach(container => { container.remove(); console.log(`已移除多余新闻容器:${container.id || container.className}`); }); } // 其他元素清理 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 => { document.querySelectorAll(selector).forEach(el => el.remove()); }); } window.addEventListener('load', () => { cleanUpNews(); removeElements(); const observer = new MutationObserver((mutations) => { let needsCleanup = false; mutations.forEach(m => { if (m.addedNodes.length > 0) needsCleanup = true; }); if (needsCleanup) { cleanUpNews(); removeElements(); } }); observer.observe(document.body, { childList: true, subtree: true }); }); } // 返回顶部按钮 function initBackToTop() { // 重新判断当前页面类型 const currentUrl = window.location.href; const isWeatherPage = currentUrl.includes('/zh-cn/weather/'); const isCalendarPage = new URL(currentUrl).pathname.startsWith('/zh-cn/lifestyle/calendar'); 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.style.display = 'none'; window.addEventListener('scroll', () => { backToTopBtn.style.display = window.scrollY > 200 ? 'flex' : 'none'; }); backToTopBtn.addEventListener('click', () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }); } // ==================== 主执行函数 ==================== function initScript() { const currentUrl = window.location.href; for (const [site, config] of Object.entries(siteConfig)) { try { const isMatch = typeof config.match === 'function' ? config.match(currentUrl) : currentUrl.match(config.match); if (isMatch) { console.log(`Applying optimizations for: ${site}`); // 添加样式 if (config.styles) { GM_addStyle(config.styles); } // 执行处理函数(如果存在) if (config.handler) { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', config.handler); } else { config.handler(); } } break; // 只匹配第一个配置 } } catch (error) { console.error(`Error processing site ${site}:`, error); } } } // ==================== 启动脚本 ==================== initScript(); initBackToTop(); })();