B&W webpage fix

Fix for dull-looking black and white webpages.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         B&W webpage fix
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Fix for dull-looking black and white webpages.
// @author       Porama Ruengrairatanaroj
// @match        *://www.mkrestaurant.com/*
// @match        *://www.tescolotus.com/*
// @match        *://www.tops.co.th/*
// @match        *://www.lazada.co.th/*
// @match        *://www.bigc.co.th/*
// @match        *://*.cpfreshmartshop.com/*
// @match        *://scbsonline.settrade.com/*
// @match        *://www.headdaddy.com/*
// @grant        none
// ==/UserScript==

window.addEventListener('load', function () {
    'use strict';

    let grayscaleRegexMatch = /grayscale\([^)]*\)/g;

    let unsetGrayscaleFilter = function(elem) {
        if (elem === null) {
            return false;
        }

        try {
            let elemStyle = window.getComputedStyle(elem);
            var watchThis = false;

            if(elemStyle === null) {
                return false;
            }

            if(elemStyle.filter) {
                let match = elemStyle.filter.match(grayscaleRegexMatch);
                if(match && match.length > 0) {
                    elem.style.setProperty("filter", "unset", "important");
                    watchThis = true;
                }
            }

            if(elemStyle.webkitFilter) {
                let match = elemStyle.webkitFilter.match(grayscaleRegexMatch);
                if(match && match.length > 0) {
                    elem.style.setProperty("-webkit-filter", "unset", "important");
                    watchThis = true;
                }
            }

            return watchThis;
        } catch (e) {
            console.log("Failed to process: " + elem + "(" + e + ")");
            return false;
        }
    };

    let filterObserver = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            console.log("Saw mutation in " + mutation.target + " of type " + mutation.type + " with attribute " + mutation.attributeName + " (ns: " + mutation.attributeNamespace + ") with oldValue " + mutation.oldValue);

            if(mutation.type === "attributes" && mutation.attributeName === "style") {
                unsetGrayscaleFilter(mutation.target);
            }
        });
    });

    let filterObserverConfig = { attributes: true, attributeOldValue: true, attributeFilter: [ 'style' ] };

    unsetGrayscaleFilter(document.body);

    // We can't limit this to <body>, since apparently some ridiculously stupid
    // websites (I'm looking at you, SCB, CP Freshmart) have <div> tags *outside*
    // of the body tag.
    let items = document.getElementsByTagName('*');
    for(var i = 0; i < items.length; ++i) {
        if(unsetGrayscaleFilter(items[i])) {
            filterObserver.observe(items[i], filterObserverConfig);
        }
    }
}, false);