Facebook Ad Preferences Cleaner

Automatically removes Facebook Ad "preferences" upon loading https://www.facebook.com/ds/preferences/

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Facebook Ad Preferences Cleaner
// @namespace    https://www.facebook.com/ds/preferences/
// @version      1.01
// @description  Automatically removes Facebook Ad "preferences" upon loading https://www.facebook.com/ds/preferences/
// @author       JustSomeGuy
// @include      https://www.facebook.com/ds/preferences/
// @grant        none
// @license      MIT
// @donation     https://www.paypal.com/paypalme/iappreciateyoutoo
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener('load', function () {
        openContainer();
        removeInterests();
    });

    var removeInterests = setInterval(function() {
        var removeDiv = getDivContainer();
        if (typeof removeDiv !== 'undefined') {
            seeMore();
            removeDiv.click();
        } else {
            clearInterval(removeInterests);
            console.log(getCategory());
            if (getCategory() !== "Removed interests") {
                location.reload();
            }
        }
    }, 250);


    function getDivContainer() {
        var baseLoc = document.getElementsByTagName('iframe')[0];
        var containerMap = [1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1];
        baseLoc = baseLoc.contentWindow.document;
        for (var i = 0, len = containerMap.length; i < len; i++) {
            baseLoc = baseLoc.childNodes[containerMap[i]];
        }
        for (var i = 0, len = baseLoc.children.length; i < len; i++) {
            if (baseLoc.childNodes[i].childNodes[0].childNodes[0].childNodes[0].childNodes[1].innerHTML == "Remove") {
                return baseLoc.childNodes[i].childNodes[0].childNodes[0].childNodes[0].childNodes[0];
            }
        }
    }

    function getCategory() {
        var categoryLoc = document.getElementsByTagName('iframe')[0];
        var containerMap = [1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        categoryLoc = categoryLoc.contentWindow.document;

        for (var i = 0, len = containerMap.length; i < len; i++) {
            categoryLoc = categoryLoc.childNodes[containerMap[i]];
        }
        return categoryLoc.innerHTML;
    }

    function seeMore() {
        var baseLoc = document.getElementsByTagName('iframe')[0];
        var containerMap = [1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 2];

        baseLoc = baseLoc.contentWindow.document;
        for (var i = 0, len = containerMap.length; i < len; i++) {
            baseLoc = baseLoc.childNodes[containerMap[i]];
        }

        setTimeout(function() {
            if (typeof(baseLoc) == 'undefined' || baseLoc == null) {
                return;
            } else {
                baseLoc.click();
                seeMore();
            }
        }, 100);
    }

    function openContainer() {
        var baseLoc = document.getElementsByTagName('iframe')[0];
        var containerMap = [1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1];
        baseLoc = baseLoc.contentWindow.document;
        for (var i = 0, len = containerMap.length; i < len; i++) {
            baseLoc = baseLoc.childNodes[containerMap[i]];
        }

        if (baseLoc.parentNode.childNodes[2].classList.contains('hidden_elem')) {
            baseLoc.click();
        }
    }
})();