<commit_before>// content script

var tcl = {
    isDEV: false,
    sessionKey: null,

    cache_rule: [
        "textarea",
        "iframe",
        "[contentEditable]",
        "[role='textbox']",
        "[aria-multiline='true']"
    ],

    except_websites: [
        "docs.google.com/spreadsheets"
    ],

    findTextContents: () => {
        var me = tcl;

        document.querySelectorAll(me.cache_rule.join(',')).forEach( (ta, i) => {
            me.isDEV && console.log('ta-class-append');
            var isTEXTAREA = ta.tagName == "TEXTAREA";
            ta.classList.add('ta-textContent');
            ta.dataset.id = isTEXTAREA ? i : `w-${i}`;
        });
        document.querySelectorAll('.ta-textContent .ta-textContent').forEach(dom => {
            me.isDEV && console.log('ta-rm-duplicate');
            dom.classList.remove('ta-textContent');
        });
    },

    attachEvents: () => {
        var me = tcl;
        var allTxtCnt = document.querySelectorAll('.ta-textContent');

        allTxtCnt.forEach( ta => {
            me.isDEV && console.log('ta-txtcnt-event');

            var saveFunction = {
                add: event => {
                    me.isDEV && console.log('ta-event-fire-focus', event);
                    event.target.addEventListener('keyup', me.saveToStorage);
                },
                remove: event => {
                    me.isDEV && console.log('ta-event-fire-blur');
                    event.target.removeEventListener('keyup', me.saveToStorage);
                }
            };
            ta.onfocus = saveFunction.add;
            ta.onblur  = saveFunction.remove;
        });
    },

    init: () => {
        var me = tcl;
        me.isDEV && console.log('ta-init');

        if (!tcl.checkEnable()) return;
        tcl.initDBTable();
        tcl.sessionKey = document.querySelector('body').dataset['taTime'] = String((new Date()).getTime());
        tcl.findTextContents();
        tcl.attachEvents();
    },

    checkEnable: () => {
        var url = location.href;
        for (var site of tcl.except_websites) {
            if (url.includes(site)) return false;
        }
        return true;
    },

    initDBTable: () => {
        browser.runtime.sendMessage({
            behavior: 'init',
            title: document.title,
            url: location.href
        });
    },

    getContent: target => {
        if (target.tagName == "TEXTAREA") {
            // textarea
            // console.log('textarea');
            return {
                val: target.value,
                isWYSIWYG: false
            };
        }
        else if (target.contentEditable) {
            // WYSIWYG
            // console.log('WYSIWYG');

            let dp = new DOMParser();
            let bodyNode = target.cloneNode(true);
            let doc = dp.parseFromString(bodyNode.innerHTML, "text/html");

            while (bodyNode.firstChild) {
                bodyNode.removeChild(bodyNode.firstChild);
            }
            doc.body.childNodes.forEach( childNode => {
                let newNode = childNode.cloneNode(true);
                bodyNode.appendChild(newNode);
            });

            /*
             * raw data ===>     bodyNode
             * save data ===>    bodyNode.outerHTML
             * output data ===>  dp.parseFromString(bodyNode.outerHTML, "text/html")
             */

            return {
                val: bodyNode.outerHTML,
                isWYSIWYG: true
            };
        }
    },

    _strip: html => html.replace(/<(?:.|\n)*?>/gm, ''),

    saveToStorage: event => {
        var save_info = tcl.getContent(event.target);

        if (tcl._strip(save_info.val).length == 0) return;

        browser.runtime.sendMessage({
            behavior: 'save',
            title: document.title,<commit_msg>fix no title problem when saving from iframe<commit_after>// content script

var tcl = {
    isDEV: false,
    sessionKey: null,

    cache_rule: [
        "textarea",
        "iframe",
        "[contentEditable]",
        "[role='textbox']",
        "[aria-multiline='true']"
    ],

    except_websites: [
        "docs.google.com/spreadsheets"
    ],

    findTextContents: () => {
        var me = tcl;

        document.querySelectorAll(me.cache_rule.join(',')).forEach( (ta, i) => {
            me.isDEV && console.log('ta-class-append');
            var isTEXTAREA = ta.tagName == "TEXTAREA";
            ta.classList.add('ta-textContent');
            ta.dataset.id = isTEXTAREA ? i : `w-${i}`;
        });
        document.querySelectorAll('.ta-textContent .ta-textContent').forEach(dom => {
            me.isDEV && console.log('ta-rm-duplicate');
            dom.classList.remove('ta-textContent');
        });
    },

    attachEvents: () => {
        var me = tcl;
        var allTxtCnt = document.querySelectorAll('.ta-textContent');

        allTxtCnt.forEach( ta => {
            me.isDEV && console.log('ta-txtcnt-event');

            var saveFunction = {
                add: event => {
                    me.isDEV && console.log('ta-event-fire-focus', event);
                    event.target.addEventListener('keyup', me.saveToStorage);
                },
                remove: event => {
                    me.isDEV && console.log('ta-event-fire-blur');
                    event.target.removeEventListener('keyup', me.saveToStorage);
                }
            };
            ta.onfocus = saveFunction.add;
            ta.onblur  = saveFunction.remove;
        });
    },

    init: () => {
        var me = tcl;
        me.isDEV && console.log('ta-init');

        if (!tcl.checkEnable()) return;
        tcl.initDBTable();
        tcl.sessionKey = document.querySelector('body').dataset['taTime'] = String((new Date()).getTime());
        tcl.findTextContents();
        tcl.attachEvents();
    },

    checkEnable: () => {
        var url = location.href;
        for (var site of tcl.except_websites) {
            if (url.includes(site)) return false;
        }
        return true;
    },

    initDBTable: () => {
        browser.runtime.sendMessage({
            behavior: 'init',
            title: document.title,
            url: location.href
        });
    },

    getContent: target => {
        if (target.tagName == "TEXTAREA") {
            // textarea
            // console.log('textarea');
            return {
                val: target.value,
                isWYSIWYG: false
            };
        }
        else if (target.contentEditable) {
            // WYSIWYG
            // console.log('WYSIWYG');

            let dp = new DOMParser();
            let bodyNode = target.cloneNode(true);
            let doc = dp.parseFromString(bodyNode.innerHTML, "text/html");

            while (bodyNode.firstChild) {
                bodyNode.removeChild(bodyNode.firstChild);
            }
            doc.body.childNodes.forEach( childNode => {
                let newNode = childNode.cloneNode(true);
                bodyNode.appendChild(newNode);
            });

            /*
             * raw data ===>     bodyNode
             * save data ===>    bodyNode.outerHTML
             * output data ===>  dp.parseFromString(bodyNode.outerHTML, "text/html")
             */

            return {
                val: bodyNode.outerHTML,
                isWYSIWYG: true
            };
        }
    },

    _strip: html => html.replace(/<(?:.|\n)*?>/gm, ''),

    saveToStorage: event => {
        var save_info = tcl.getContent(event.target);

        if (tcl._strip(save_info.val).length == 0) return;

        browser.runtime.sendMessage({
            behavior: 'save',
            title: window.parent.document.title,
{'fraction_non_alphanumeric': 0.1438774185170757, 'fraction_numerical': 0.0002597065316192702, 'mean_word_length': 1.555408095554081, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, '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': '184199', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2180, 'n_words': 362}