<commit_before>/* The background process for chrome */

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.message == 'settingsclick'){
        // open new tab
        chrome.tabs.create(
            {url: chrome.extension.getURL('data/settings/index.html')}
        );
    }
});

// TODO: use storage.sync for the cloud sync key!!!
// use storage.local for everything else

// Initialize preferences
var default_prefs = {
    'autofilter': true, 
    'tags': [], 
    'last_sync':0, 
    'sync_enabled':true
};


var storage = chrome.storage.local;

storage.get("prefs", function (items){
    if (!items.prefs)
        chrome.storage.local.set({'prefs': default_prefs});
});

function handleNewFic(metadata, mutable_data, port) {
/* Take in the data and rating, and store or update as necessary. Returns
   the new object.
*/
    if (!metadata['ao3id']){
        // Must have a vailid ID!
        return null;
    }
    var newArticle = new Article(metadata, mutable_data);
    // We only want to create a new entry if the mutable data is more than just visit
    var create_if_ne = false;
    if (mutable_data){
        var mutable_keys = Object.keys(mutable_data);
        create_if_ne = !arrayCompare(mutable_keys, ['visit']);
    }
    saveArticle(newArticle, create_if_ne, port, true);
}

function saveArticle(newArticle, create_if_ne, port, do_sync){
    var storage = chrome.storage.local;
    // WARNING: CHECK FOR VAILD ao3id
    // ASKING FOR undefined IN LOCALSTORAGE RETURNS EVERYTHING
    console.assert(newArticle.ao3id);
    if (!newArticle.ao3id){
        return;
    }

    storage.get(newArticle.ao3id, function (data){
        var old_article = data[newArticle.ao3id];
        if (!old_article){
            if (!create_if_ne){
                return null;
            }
            data[newArticle.ao3id] = newArticle;
        } else {
            var updated_article = updateArticle(old_article, newArticle);
            if (updated_article){
                data[updated_article.ao3id] = updated_article;
            }
        }
        // Save the data
        chrome.storage.local.set( data );

        // Sync to server (the function handles checking for permission)
        if (do_sync){
            syncWork(data[newArticle.ao3id]);
        }
        
        // if (port){
        //     port.postMessage({message: 'newfic', data: data[newArticle.ao3id]});
        // }
        // Broadcast new article changes
        broadcast({message: 'newfic', data: data[newArticle.ao3id]});
    });
}


function savePrefs(prefs){
    var storage = chrome.storage.local;

    storage.get("prefs", function (items){
        var tags_changed = false;
        for (var key in prefs){
            if ((key == 'tags') && typeof prefs[key] === 'string') {
                tags_changed = (items.prefs[key].toString() != prefs[key].split(',').toString());
                items.prefs[key] = prefs[key].split(',');
            } else {
                items.prefs[key] = prefs[key];
            }
        }
        chrome.storage.local.set( items );
        // Only broadcast if tags changed
        if (tags_changed){
            broadcast({message: 'datadump', data: items, data_type:'prefs'});
        }
    });
}

function broadcast(message){
    "Broadcast a message to ALL tabs. No callbacks allowed. Tested this with "
    "enough tabs open to make my computer sluggish. Didn't seem to make it worse,"
    "I assume this is a really light weight function."
    chrome.tabs.query({}, function(tabs) {
        for (var idx in tabs) {
            if (tabs[idx].id){
                chrome.tabs.sendMessage(tabs[idx].id, message);
            }
        }
    });
}

// NOTE! You have to call this twice to get it to send
// first with the port, then with the data you want to send.
var callbackMessage = (function(port){
    return function(message, data, data_type) {
        port.postMessage({'message': message, 'data': data, 'data_type': data_type});
    };
});

chrome.runtime.onConnect.addListener(function(port) {
    // if (!(port.name == "articles-table")){
    //     return;
    // }
    port.onMessage.addListener(function(request) {
        if (request.message == "reveal-token"){
            getUser(callbackMessage(port));
        }

        if (request.message == 'save-token'){
            validateAndSaveToken(request.data, port);
        }

        if (request.message == 'prefs') {
            savePrefs(request.data);
        }

        if (request.message == 'fetchdata') {
            fetchDataRequest(request, port);
        }

        if (request.message == 'restorefrombackup'){
            // Update the DB data
            var version = request.data['version'];
            var article_data = request.data['article_data'];

            for (var key in article_data){
                if (article_data.hasOwnProperty(key) && article_data[key]['ao3id']) {
                    saveArticle(article_data[key], true, port, true);
                }
            }
            savePrefs(request.data['prefs']);

        }

        if (request.message == 'ficdata'){
            handleNewFic(request.data.metadata, request.data.mutable_data, port);
        }

        if (request.message == 'runsync'){
            // TODO: is this the best place for this?
            runSync();
        }
    });
});<commit_msg>Data import issue for chrome. Fixes #14.<commit_after>/* The background process for chrome */

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.message == 'settingsclick'){
        // open new tab
        chrome.tabs.create(
            {url: chrome.extension.getURL('data/settings/index.html')}
        );
    }
});

// TODO: use storage.sync for the cloud sync key!!!
// use storage.local for everything else

// Initialize preferences
var default_prefs = {
    'autofilter': true, 
    'tags': [], 
    'last_sync':0, 
    'sync_enabled':true
};


var storage = chrome.storage.local;

storage.get("prefs", function (items){
    if (!items.prefs)
        chrome.storage.local.set({'prefs': default_prefs});
});

function handleNewFic(metadata, mutable_data, port) {
/* Take in the data and rating, and store or update as necessary. Returns
   the new object.
*/
    if (!metadata['ao3id']){
        // Must have a vailid ID!
        return null;
    }
    var newArticle = new Article(metadata, mutable_data);
    // We only want to create a new entry if the mutable data is more than just visit
    var create_if_ne = false;
    if (mutable_data){
        var mutable_keys = Object.keys(mutable_data);
        create_if_ne = !arrayCompare(mutable_keys, ['visit']);
    }
    saveArticle(newArticle, create_if_ne, port, true);
}

function saveArticle(newArticle, create_if_ne, port, do_sync){
    var storage = chrome.storage.local;
    // WARNING: CHECK FOR VAILD ao3id
    // ASKING FOR undefined IN LOCALSTORAGE RETURNS EVERYTHING
    console.assert(newArticle.ao3id);
    if (!newArticle.ao3id){
        return;
    }

    storage.get(newArticle.ao3id, function (data){
        var old_article = data[newArticle.ao3id];
        if (!old_article){
            if (!create_if_ne){
                return null;
            }
            data[newArticle.ao3id] = newArticle;
        } else {
            var updated_article = updateArticle(old_article, newArticle);
            if (updated_article){
                data[updated_article.ao3id] = updated_article;
            }
        }
        // Save the data
        chrome.storage.local.set( data );

        // Sync to server (the function handles checking for permission)
        if (do_sync){
            syncWork(data[newArticle.ao3id]);
        }
        
        // if (port){
        //     port.postMessage({message: 'newfic', data: data[newArticle.ao3id]});
        // }
        // Broadcast new article changes
        broadcast({message: 'newfic', data: data[newArticle.ao3id]});
    });
}


function savePrefs(prefs){
    var storage = chrome.storage.local;

    storage.get("prefs", function (items){
        var tags_changed = false;
        for (var key in prefs){
            if ((key == 'tags') && typeof prefs[key] === 'string') {
                tags_changed = (items.prefs[key].toString() != prefs[key].split(',').toString());
                items.prefs[key] = prefs[key].split(',');
            } else {
                items.prefs[key] = prefs[key];
            }
        }
        chrome.storage.local.set( items );
        // Only broadcast if tags changed
        if (tags_changed){
            broadcast({message: 'datadump', data: items, data_type:'prefs'});
        }
    });
}

function broadcast(message){
    "Broadcast a message to ALL tabs. No callbacks allowed. Tested this with "
    "enough tabs open to make my computer sluggish. Didn't seem to make it worse,"
    "I assume this is a really light weight function."
    chrome.tabs.query({}, function(tabs) {
        for (var idx in tabs) {
            if (tabs[idx].id){
                chrome.tabs.sendMessage(tabs[idx].id, message);
            }
        }
    });
}

// NOTE! You have to call this twice to get it to send
// first with the port, then with the data you want to send.
var callbackMessage = (function(port){
    return function(message, data, data_type) {
        port.postMessage({'message': message, 'data': data, 'data_type': data_type});
    };
});

chrome.runtime.onConnect.addListener(function(port) {
    // if (!(port.name == "articles-table")){
    //     return;
    // }
    port.onMessage.addListener(function(request) {
        if (request.message == "reveal-token"){
            getUser(callbackMessage(port));
        }

        if (request.message == 'save-token'){
            validateAndSaveToken(request.data, port);
        }

        if (request.message == 'prefs') {
            savePrefs(request.data);
        }

        if (request.message == 'fetchdata') {
            fetchDataRequest(request, port);
        }

        if (request.message == 'restorefrombackup'){
            // Update the DB data
            var version = request.data['version'];
            var article_data = request.data['ficdict'];

            for (var key in article_data){
                if (article_data.hasOwnProperty(key) && article_data[key]['ao3id']) {
                    saveArticle(article_data[key], true, port, true);
                }
            }
            savePrefs(request.data['prefs']);

        }

        if (request.message == 'ficdata'){
            handleNewFic(request.data.metadata, request.data.mutable_data, port);
        }

        if (request.message == 'runsync'){
            // TODO: is this the best place for this?
            runSync();
        }
    });
});
{'fraction_non_alphanumeric': 0.12935185185185186, 'fraction_numerical': 0.0025925925925925925, 'mean_word_length': 2.175830638047633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6371614', 'n_tokens_mistral': 3231, 'n_tokens_neox': 3125, 'n_words': 790}