<commit_before>//
// content.js
//

'use strict';

Object.defineProperty(Node.prototype, 'getBackgroundImages', {
    value: function(x, y) {
        const elements = this.elementsFromPoint(x, y).filter(
            element => element instanceof Element
        );

        const images = elements.reduce((acc, element) => {
            const array = [
                element.getImageFromStyle(),
                element.getImageFromStyle('::before'),
                element.getImageFromStyle('::after'),
                element.getImageFromSrc()
            ];

            // Disabled because Shadow DOM is unstable
            // See https://github.com/foooomio/view-background-image/issues/12
            /*
            if (element.shadowRoot) {
                return acc.concat(
                    array, element.shadowRoot.getBackgroundImages(x, y)
                );
            }
            */

            return acc.concat(array);
        }, []);

        // Unique and Compact
        return images.filter((value, index, array) =>
            value && array.indexOf(value) === index
        );
    }
});

Object.defineProperties(Element.prototype, {
    getImageFromStyle: {
        value: function(pseudo) {
            const style = getComputedStyle(this, pseudo);
            const value = style.getPropertyValue('background-image');
            const matches = /url\("?(.+?)"?\)/.exec(value);
            return matches ? matches[1] : null;
        }
    },
    getImageFromSrc: {
        value: function() {
            if (this.tagName === 'IMG') {
                return this.currentSrc || this.src;
            }
        }
    }
});

{
    let images = [];

    document.addEventListener('contextmenu', e => {
        images = document.getBackgroundImages(e.clientX, e.clientY);
    });

    chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
        sendResponse(images.join(' '));
    });
}
<commit_msg>Fix stackoverflow on shadow dom<commit_after>//
// content.js
//

'use strict';

Object.defineProperty(Node.prototype, 'getBackgroundImages', {
    value: function(x, y) {
        const elements = this.elementsFromPoint(x, y).filter(element =>
            element instanceof Element && element.getRootNode() === this
        );

        const images = elements.reduce((acc, element) => {
            const array = [
                element.getImageFromStyle(),
                element.getImageFromStyle('::before'),
                element.getImageFromStyle('::after'),
                element.getImageFromSrc()
            ];

            if (element.shadowRoot) {
                return acc.concat(
                    array, element.shadowRoot.getBackgroundImages(x, y)
                );
            }

            return acc.concat(array);
        }, []);

        // Unique and Compact
        return images.filter((value, index, array) =>
            value && array.indexOf(value) === index
        );
    }
});

Object.defineProperties(Element.prototype, {
    getImageFromStyle: {
        value: function(pseudo) {
            const style = getComputedStyle(this, pseudo);
            const value = style.getPropertyValue('background-image');
            const matches = /url\("?(.+?)"?\)/.exec(value);
            return matches ? matches[1] : null;
        }
    },
    getImageFromSrc: {
        value: function() {
            if (this.tagName === 'IMG') {
                return this.currentSrc || this.src;
            }
        }
    }
});

{
    let images = [];

    document.addEventListener('contextmenu', e => {
        images = document.getBackgroundImages(e.clientX, e.clientY);
    });

    chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
        sendResponse(images.join(' '));
    });
}

{'fraction_non_alphanumeric': 0.13716697441308362, 'fraction_numerical': 0.0010551305724083356, 'mean_word_length': 1.8130563798219586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2076278', 'n_tokens_mistral': 1029, 'n_tokens_neox': 995, 'n_words': 181}