Remove promoted questions and answers on Quora

Removes promoted answers that have nothing to do with the question you're looking up

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Remove promoted questions and answers on Quora
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Removes promoted answers that have nothing to do with the question you're looking up
// @author       https://greasyfork.org/en/users/728793-keyboard-shortcuts
// @match        https://www.quora.com/*
// @match        https://quora.com/*
// @icon         https://www.google.com/s2/favicons?domain=quora.com&sz=128
// @grant        none
// @license      MIT
// ==/UserScript==

/* jshint esversion: 6 */

(function() {
    'use strict';
    var logRemovalsToDevConsole = false; // Change this to true to log removals to the console

    function selectNodesWithXpath(selector) {
        const xpathResult = document.evaluate(selector, document, null, XPathResult.ANY_TYPE, null);

        const elements = [];
        var curElement;
        while (xpathResult && (curElement = xpathResult.iterateNext()) !== null) {
            elements.push(curElement);
        }
        return elements;
    }

    setInterval(function() {
        const removedClass = '__quora_ad_removed__';

        // search for the "Promoted" or "Sponsored" string in a block with a small font ("q-text" or "q-click-wrapper")
        const elements = selectNodesWithXpath(`//div[(contains(@class, 'q-text') or contains(@class, 'q-click-wrapper')) and (not(contains(@class, '${removedClass}'))) and (text() = 'Promoted' or text() = 'Sponsored')]`);

        const toRemove = [];
        var textBlock = null;
        for (const textBlock of elements) {
            var node = textBlock;

            // find the first parent node with a class containing the string "Card_", this is the block we want to hide
            do {
                node = node.parentNode;
            } while (node && (node.className || '').indexOf('Card_') === -1);

            if (node) { // found!
                if (logRemovalsToDevConsole) {
                    console.log('Removing promoted block', node);
                }
                textBlock.classList.add(removedClass); // avoid picking up the same ad on the next page scan
                node.style.display = 'none';
            }
        }
    }, 250); // repeat as more results are loaded (every 250ms)
})();