Greasy Fork

Greasy Fork is available in English.

新版巴哈姆特干净化 & 扩宽显示区域

清理新版巴哈姆特的广告(首页、GNN新闻、哈拉区)、扩宽显示区域(首页、GNN新闻、哈拉区)、移除页脚与无用的浮动区块

当前为 2025-05-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         New Bahamut Cleaner & Content Widening
// @name:zh-TW   新版巴哈姆特乾淨化 & 擴寬顯示區域
// @name:zh-CN   新版巴哈姆特干净化 & 扩宽显示区域
// @namespace    https://www.tampermonkey.net/
// @version      2.5
// @description  Cleans up ads on the new version of Bahamut (Homepage, GNN News, and Forum), widens the content display area (Homepage, GNN News, and Forum), and removes the footer and unnecessary floating elements.
// @description:zh-TW 清理新版巴哈姆特的廣告(首頁、GNN新聞、哈拉區)、擴寬顯示區域(首頁、GNN新聞、哈拉區)、移除頁腳與無用的浮動區塊
// @description:zh-CN 清理新版巴哈姆特的广告(首页、GNN新闻、哈拉区)、扩宽显示区域(首页、GNN新闻、哈拉区)、移除页脚与无用的浮动区块
// @author       ChatGPT
// @match        https://www.gamer.com.tw/*
// @match        https://gnn.gamer.com.tw/*
// @match        https://forum.gamer.com.tw/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  // ===== 首頁區模組 =====
  const cleanBahaAds = () => {
    const bannerImg = document.querySelector('#adunit img[alt="超級看板廣告"]');
    if (bannerImg) {
      const adContainer = bannerImg.closest('div.GoogleActiveViewElement');
      if (adContainer) adContainer.remove();
    }

    const gapContainer = document.querySelector('#billboardAd.BH-banner--lg');
    if (gapContainer) gapContainer.remove();

    const rightAdItems = document.querySelectorAll('.main-container__right .BH-banner__item');
    rightAdItems.forEach(item => item.remove());

    const bannerRow = document.querySelector('.main-container__right .BH-banner__row');
    if (bannerRow) {
      bannerRow.style.minHeight = '0';
      bannerRow.style.padding = '0';
      bannerRow.style.margin = '0';
    }

    const fuliSection = document.querySelector('div.main-container__right > section.index-fuli');
    if (fuliSection) fuliSection.remove();

    const fixedRightLinks = document.querySelectorAll('div.fixed-right div.column a.fixed-right__link');
    fixedRightLinks.forEach(link => link.remove());

    const mainRow = document.querySelector('main.main-container__row');
    if (mainRow) {
      mainRow.style.width = '126%';
      mainRow.style.margin = '0 auto';
    }

    const mainContent = document.querySelector('.main-index__content');
    if (mainContent) {
      mainContent.style.width = '100%';
      mainContent.style.maxWidth = '1350px';
    }

    const postPanel = document.querySelector('#postPanel.section-index');
    if (postPanel) {
      const adWrappers = postPanel.querySelectorAll('.ad__wrapper');
      adWrappers.forEach(ad => {
        if (ad.parentElement?.children.length > 1) {
          ad.style.display = 'none';
        }
      });
    }

    if (!document.querySelector('div.pswp')) {
      const pswp = document.createElement('div');
      pswp.className = 'pswp';
      document.body.appendChild(pswp);
    }

    const footer = document.querySelector('footer.main-container__footer');
    if (footer) footer.remove();
  };

  // ===== 新聞區模組 =====
  const cleanGNN = () => {
    const shop = document.querySelector('div#shop.lazyloaded');
    if (shop) shop.remove();

    const banner4gamer = document.querySelector('div#banner-4gamer');
    if (banner4gamer) banner4gamer.remove();

    const gptBanner = document.querySelector('div#div-gpt-ad-banner');
    if (gptBanner) gptBanner.remove();

    const googleImageDiv = document.querySelector('div#google_image_div.GoogleActiveViewElement');
    if (googleImageDiv) googleImageDiv.remove();

    // 移除 GN-lbox2B 中的廣告元素(判斷內含廣告關鍵字)
    document.querySelectorAll('div.GN-lbox2B').forEach(el => {
      const id = el.id || '';
      const html = el.innerHTML;
      const isAd = /ad|ads|gpt|google/i.test(id) || /google_ads_iframe|gpt|doubleclick|adservice|googlesyndication/i.test(html);
      if (isAd) el.remove();
    });

    const flySalve = document.querySelector('div#flySalve');
    if (flySalve) flySalve.remove();

    const bhSlave = document.querySelector('div#BH-slave');
    if (bhSlave) bhSlave.remove();

    const bhWrapper = document.querySelector('div#BH-wrapper');
    if (bhWrapper) bhWrapper.style.maxWidth = '100%';

    const bhMaster = document.querySelector('div#BH-master');
    if (bhMaster) bhMaster.style.width = 'calc(100%)';

    const gnTopimg = document.querySelector('div#BH-master div.GN-topimg');
    if (gnTopimg) gnTopimg.style.width = '100%';
  };

  // ===== 哈拉區模組:廣告清除 =====
  const cleanForumAds = () => {
    // 移除插入在列表中的所有廣告列
    const rows = document.querySelectorAll('form div.b-list-wrap table.b-list tbody tr.b-list__row');
    rows.forEach(row => {
      if (row.querySelector('td.b-list_ad')) row.remove();
    });

    // 移除頂部大型橫幅廣告
    const topAd = document.querySelector('div#div-gpt-ad-banner');
    if (topAd) topAd.remove();

    const googleImageAd = document.querySelector('div#google_image_div.GoogleActiveViewElement');
    if (googleImageAd) googleImageAd.remove();

    // 移除右側 flySalve 廣告容器
    const flySalve = document.querySelector('div#flySalve');
    if (flySalve) flySalve.remove();
  };

  // ===== 哈拉區模組:加寬 C.php 貼文頁 =====
  const widenForumCPage = () => {
    const bhWrapper = document.querySelector('#BH-wrapper');
    if (bhWrapper) bhWrapper.style.width = '100%';

    const style = document.createElement('style');
    style.textContent = `
      #BH-master {
        width: auto !important;
      }
      .c-section__main {
        float: right;
        width: 1440px !important;
        text-align: left;
        box-sizing: border-box;
      }
    `;
    document.head.appendChild(style);
  };

  // ===== 哈拉區模組:加寬 B.php 列表頁 =====
  const widenForumBPage = () => {
    const bhWrapper = document.querySelector('#BH-wrapper');
    if (bhWrapper) bhWrapper.style.width = '100%';

    const style = document.createElement('style');
    style.textContent = `
      #BH-master {
        width: 1615px !important;
      }
    `;
    document.head.appendChild(style);
  };

  // ===== 執行首頁區模組 =====
  if (location.hostname === 'www.gamer.com.tw') {
    setTimeout(() => cleanBahaAds(), 10);
    const mainContent = document.querySelector('.main-index__content');
    if (mainContent) {
      let debounceTimer1 = null;
      const observer1 = new MutationObserver(() => {
        clearTimeout(debounceTimer1);
        debounceTimer1 = setTimeout(() => cleanBahaAds(), 10);
      });
      observer1.observe(mainContent, { childList: true, subtree: true });
    }
  }

  // ===== 執行新聞區模組 =====
  if (location.hostname === 'gnn.gamer.com.tw') {
    setTimeout(() => cleanGNN(), 10);
    const bhWrapper = document.querySelector('div#BH-wrapper');
    if (bhWrapper) {
      let debounceTimer2 = null;
      const observer2 = new MutationObserver(() => {
        clearTimeout(debounceTimer2);
        debounceTimer2 = setTimeout(() => cleanGNN(), 10);
      });
      observer2.observe(bhWrapper, { childList: true, subtree: true });
    }
  }

  // ===== 執行哈拉區模組(廣告清除 + 依網址加寬)=====
  if (location.hostname === 'forum.gamer.com.tw') {
    setTimeout(() => {
      cleanForumAds();
      const pathname = location.pathname;
      if (pathname.startsWith('/C.php')) {
        widenForumCPage();
      } else if (pathname.startsWith('/B.php')) {
        widenForumBPage();
      }
    }, 10);

    const listWrap = document.querySelector('form div.b-list-wrap');
    if (listWrap) {
      let debounceTimer3 = null;
      const observer3 = new MutationObserver(() => {
        clearTimeout(debounceTimer3);
        debounceTimer3 = setTimeout(() => {
          cleanForumAds();
          const pathname = location.pathname;
          if (pathname.startsWith('/C.php')) {
            widenForumCPage();
          } else if (pathname.startsWith('/B.php')) {
            widenForumBPage();
          }
        }, 10);
      });
      observer3.observe(listWrap, { childList: true, subtree: true });
    }
  }
})();