[Chrome] 使用 Tampermonkey 過濾掉 Google 新聞中,不想看到的文章(2025新版)

[Chrome] 使用 Tampermonkey 過濾掉 Google 新聞中,不想看到的文章(2025新版)

之前寫過一篇 [Chrome] 使用 Tampermonkey 過濾掉 Google 新聞中,不想看到的文章

不過後來 Google 新聞改版了,原本的 Tampermonkey script 也失效。

新版的 Google 新聞裡可以設定不要看哪些媒體,但不能設關鍵字過濾。

有時看到某些人的新聞,還是覺得很浪費時間。

 

上次的 script 是自己寫的,

這次就改用 ChatGPT 來產出 Tampermonkey script 囉~

其實效果還不錯,先開出需求後,再請它修正一下,

執行時發現了錯誤訊息也丟給它,它就再改了一版,

我也再手動改了一些,就完成囉~

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// ==UserScript==
// @name Google News Filter by Any Text
// @namespace http://tampermonkey.net/
// @version 1.2
// @author ephrain
// @match https://news.google.com/home*
// @icon https://news.google.com/favicon.ico
// @grant none
// ==/UserScript==
(function() {
'use strict';
const keywords = [
"館長",
"馬英九"
];
let observer = null;
function articleContainsKeyword(article) {
const text = article.innerText;
return keywords.find(keyword => text.includes(keyword));
}
function replaceWithNotice(article, keyword) {
if (observer) {
observer.disconnect();
}
// Remove all children safely
while (article.firstChild) {
article.removeChild(article.firstChild);
}
// Create a safe replacement block
const notice = document.createElement('div');
notice.textContent = `關於[${keyword}]的新聞已被隱藏`;
Object.assign(notice.style, {
fontSize: '16px',
color: '#888',
padding: '1em',
border: '1px dashed #ccc',
backgroundColor: '#f9f9f9',
textAlign: 'center',
fontStyle: 'italic'
});
article.appendChild(notice);
if (observer) {
observer.observe(document.body, {
childList: true,
subtree: true
});
}
}
function filterArticles() {
const articles = document.querySelectorAll('article');
articles.forEach(article => {
let keyword = articleContainsKeyword(article);
if (keyword) {
replaceWithNotice(article, keyword);
}
});
}
// Watch for dynamically added articles
observer = new MutationObserver(() => {
filterArticles();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();
// ==UserScript== // @name Google News Filter by Any Text // @namespace http://tampermonkey.net/ // @version 1.2 // @author ephrain // @match https://news.google.com/home* // @icon https://news.google.com/favicon.ico // @grant none // ==/UserScript== (function() { 'use strict'; const keywords = [ "館長", "馬英九" ]; let observer = null; function articleContainsKeyword(article) { const text = article.innerText; return keywords.find(keyword => text.includes(keyword)); } function replaceWithNotice(article, keyword) { if (observer) { observer.disconnect(); } // Remove all children safely while (article.firstChild) { article.removeChild(article.firstChild); } // Create a safe replacement block const notice = document.createElement('div'); notice.textContent = `關於[${keyword}]的新聞已被隱藏`; Object.assign(notice.style, { fontSize: '16px', color: '#888', padding: '1em', border: '1px dashed #ccc', backgroundColor: '#f9f9f9', textAlign: 'center', fontStyle: 'italic' }); article.appendChild(notice); if (observer) { observer.observe(document.body, { childList: true, subtree: true }); } } function filterArticles() { const articles = document.querySelectorAll('article'); articles.forEach(article => { let keyword = articleContainsKeyword(article); if (keyword) { replaceWithNotice(article, keyword); } }); } // Watch for dynamically added articles observer = new MutationObserver(() => { filterArticles(); }); observer.observe(document.body, { childList: true, subtree: true }); })();
// ==UserScript==
// @name         Google News Filter by Any Text
// @namespace    http://tampermonkey.net/
// @version      1.2
// @author       ephrain
// @match        https://news.google.com/home*
// @icon         https://news.google.com/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const keywords = [
        "館長",
        "馬英九"
    ];

    let observer = null;

    function articleContainsKeyword(article) {
        const text = article.innerText;
        return keywords.find(keyword => text.includes(keyword));
    }

    function replaceWithNotice(article, keyword) {
        if (observer) {
            observer.disconnect();
        }

        // Remove all children safely
        while (article.firstChild) {
            article.removeChild(article.firstChild);
        }

        // Create a safe replacement block
        const notice = document.createElement('div');
        notice.textContent = `關於[${keyword}]的新聞已被隱藏`;
        Object.assign(notice.style, {
            fontSize: '16px',
            color: '#888',
            padding: '1em',
            border: '1px dashed #ccc',
            backgroundColor: '#f9f9f9',
            textAlign: 'center',
            fontStyle: 'italic'
        });

        article.appendChild(notice);

        if (observer) {
            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        }
    }

    function filterArticles() {
        const articles = document.querySelectorAll('article');
        articles.forEach(article => {
            let keyword = articleContainsKeyword(article);
            if (keyword) {
                replaceWithNotice(article, keyword);
            }
        });
    }

    // Watch for dynamically added articles
    observer = new MutationObserver(() => {
        filterArticles();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

})();

 

來看看這個的效果吧~

把不想要看到的新聞關鍵字,放進 keywords 變數裡,

那麼不想要看到的新聞就會被隱藏起來。

如果連那些字都不想看到的話,

修改一下程式裡的  

關於[${keyword}]的新聞已被隱藏
關於[${keyword}]的新聞已被隱藏 的字句就可以了:

 

(本頁面已被瀏覽過 37 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料