<commit_before>'use strict';
import { generate, isMasterPw } from "./generate.js";
// State I want to keep around that doesn't appear in the file system
const sitedataBookmark = "SitePasswordData";
const logging = false;
var bg = {};
var masterpw = "";
var activetab;
const databaseDefault = { "domains": {}, "sites": {} };
var database = clone(databaseDefault);
var domainname = "";
var protocol = "";
var pwcount = 0;
var createBookmarksFolder = true;
var pwfielddomain = {};
export const config = {
    lower: "abcdefghijklmnopqrstuvwxyz",
    upper: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    digits: "0123456789",
    specials: "/!=@?._-",
    miniter: 100,
    maxiter: 1000
};
export const defaultSettings = {
    sitename: "",
    username: "",
    pwlength: 12,
    domainname: "",
    startwithletter: true,
    allowlower: true,
    allowupper: true,
    allownumber: true,
    allowspecial: false,
    minlower: 1,
    minupper: 1,
    minnumber: 1,
    minspecial: 0,
    specials: config.specials,
};

if (logging) console.log("bg clear masterpw");

if (logging) console.log("bg starting with database", database);

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (logging) console.log(Date.now(), "bg got message request, sender", request, sender);
    retrieveMetadata(sendResponse, () => {
        if (logging) console.log("bg listener back from retrieveMetadata", database);
        chrome.storage.session.get(["ssp"], (ssp) => {
            if (ssp.ssp) {
                masterpw = ssp.ssp.masterpw || "";
                bg.masterpw = masterpw;
                if (logging) console.log("bg got ssp");
            }
            if (request.cmd === "getMetadata") {
                getMetadata(request, sender, sendResponse);
            } else if (request.cmd === "siteData") {
                if (logging) console.log("bg got site data", request);
                bg = clone(request.bg);
                masterpw = bg.masterpw;
                database.clearmasterpw = request.clearmasterpw;
                database.sites[request.sitename] = bg.settings;
                persistMetadata(sendResponse);
            } else if (request.cmd === "getPassword") {
                let origin = getdomainname(sender.origin);
                domainname = pwfielddomain[origin] || origin;
                bg.settings = bgsettings(domainname);
                let p = generate(bg);
                if (database.clearmasterpw) {
                    masterpw = "";
                    bg.masterpw = "";
                    persistMetadata(sendResponse);
                }
                if (logging) console.log("bg calculated sitepw", bg, database, p, isMasterPw(masterpw));
                sendResponse(p);
            } else if (request.clicked) {
                domainname = getdomainname(sender.origin);
                bg.domainname = domainname;
                if (logging) console.log("bg clicked: sending response", bg);
                sendResponse(bg);
                if (database.clearmasterpw) {
                    masterpw = "";
                    if (logging) console.log("bg clear masterpw")
                }
            } else if (request.onload) {
                onContentPageload(request, sender, sendResponse);
                persistMetadata(sendResponse);
            }
            if (logging) console.log(Date.now(), "bg addListener returning", isMasterPw(masterpw));
        });
    });
    return true;
});
async function getMetadata(request, _sender, sendResponse) {
    if (logging) console.log("bg getMetadata", bg, request);
    let sitename = database.domains[request.domainname];
    if (sitename) {
        bg.settings = database.sites[sitename];
    } else {
        bg.settings = clone(defaultSettings);
    }
    // Domain name comes from popup, which is trusted not to spoof it
    bg.settings.domainname = pwfielddomain[request.domainname] || request.domainname;
    if (logging) console.log("bg sending metadata", pwcount, bg, database);
    sendResponse({ "masterpw": masterpw || "", "pwcount": pwcount, "bg": bg, "database": database });
}
function onContentPageload(request, sender, sendResponse) {
    if (logging) console.log("bg onContentPageLoad", request);
    activetab = sender.tab;
    bg.pwcount = request.count;
    pwcount = bg.pwcount;
    if (pwcount > 0) {
        pwfielddomain[getdomainname(sender.origin)] = getdomainname(sender.tab.url);
    }
    let origin = getdomainname(sender.origin);
    domainname = pwfielddomain[origin] || origin;
    if (logging) console.log("bg pwcount, domainname, masterpw", database, bg.pwcount, domainname, isMasterPw(masterpw));
    let sitename = database.domains[domainname];
    if (sitename) {
        bg.settings = database.sites[sitename];
    } else {
        bg.settings = clone(defaultSettings);
    }
    bg.settings.domainname = domainname;
    let readyForClick = false;
    if (masterpw && bg.settings.sitename && bg.settings.username) {
        readyForClick = true;
    }
    let sitepass = "";
    if (bg.pwcount !== 0 && bg.settings.username) {
        let p = generate(bg);
        sitepass = p;
    }
    if (logging) console.log(Date.now(), "bg send response", { cmd: "fillfields", "u": bg.settings.username || "", "p": sitepass, "readyForClick": readyForClick });
    sendResponse({ cmd: "fillfields", "u": bg.settings.username || "", "p": sitepass, "readyForClick": readyForClick });
}
async function persistMetadata(sendResponse) {
    // localStorage[name] = JSON.stringify(value);
    if (logging) console.log("bg persistMetadata", bg, database);
    chrome.storage.session.set({ "ssp": { "masterpw": masterpw } });
    let found = await getRootFolder(sendResponse);
    if (found.length > 1) return;
    let rootFolder = found[0];
    let allchildren = await chrome.bookmarks.getChildren(rootFolder.id);;
    if (bg.settings.sitename) {
        database.domains[bg.settings.domainname] = bg.settings.sitename;
        database.sites[bg.settings.sitename] = bg.settings;
    } else if (bg.settings.domainname) {
        let sitename = database.domains[bg.settings.domainname];
        delete database.domains[bg.settings.domainname];
        // Delete the item in domain.sites if there are no entries in
        // database.domains that refers to it
        if (Object.values(database.domains).indexOf(sitename) == -1) {
            delete database.sites[sitename];
        }
        for (let i = 0; i < allchildren.length; i++) {
            if (allchildren[i].title === bg.settings.domainname) chrome.bookmarks.remove(allchildren[i].id, (_e) => {
                if (logging) console.log("bg removed settings bookmark", _e, allchildren[i]);
            });
        }
    }
    if (database && database.sites && database.sites[""] || database.sites["undefined"]) {
        console.log("bg bad sitename", database);<commit_msg>Use includes instead of indexOf<commit_after>'use strict';
import { generate, isMasterPw } from "./generate.js";
// State I want to keep around that doesn't appear in the file system
const sitedataBookmark = "SitePasswordData";
const logging = false;
var bg = {};
var masterpw = "";
var activetab;
const databaseDefault = { "domains": {}, "sites": {} };
var database = clone(databaseDefault);
var domainname = "";
var protocol = "";
var pwcount = 0;
var createBookmarksFolder = true;
var pwfielddomain = {};
export const config = {
    lower: "abcdefghijklmnopqrstuvwxyz",
    upper: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    digits: "0123456789",
    specials: "/!=@?._-",
    miniter: 100,
    maxiter: 1000
};
export const defaultSettings = {
    sitename: "",
    username: "",
    pwlength: 12,
    domainname: "",
    startwithletter: true,
    allowlower: true,
    allowupper: true,
    allownumber: true,
    allowspecial: false,
    minlower: 1,
    minupper: 1,
    minnumber: 1,
    minspecial: 0,
    specials: config.specials,
};

if (logging) console.log("bg clear masterpw");

if (logging) console.log("bg starting with database", database);

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (logging) console.log(Date.now(), "bg got message request, sender", request, sender);
    retrieveMetadata(sendResponse, () => {
        if (logging) console.log("bg listener back from retrieveMetadata", database);
        chrome.storage.session.get(["ssp"], (ssp) => {
            if (ssp.ssp) {
                masterpw = ssp.ssp.masterpw || "";
                bg.masterpw = masterpw;
                if (logging) console.log("bg got ssp");
            }
            if (request.cmd === "getMetadata") {
                getMetadata(request, sender, sendResponse);
            } else if (request.cmd === "siteData") {
                if (logging) console.log("bg got site data", request);
                bg = clone(request.bg);
                masterpw = bg.masterpw;
                database.clearmasterpw = request.clearmasterpw;
                database.sites[request.sitename] = bg.settings;
                persistMetadata(sendResponse);
            } else if (request.cmd === "getPassword") {
                let origin = getdomainname(sender.origin);
                domainname = pwfielddomain[origin] || origin;
                bg.settings = bgsettings(domainname);
                let p = generate(bg);
                if (database.clearmasterpw) {
                    masterpw = "";
                    bg.masterpw = "";
                    persistMetadata(sendResponse);
                }
                if (logging) console.log("bg calculated sitepw", bg, database, p, isMasterPw(masterpw));
                sendResponse(p);
            } else if (request.clicked) {
                domainname = getdomainname(sender.origin);
                bg.domainname = domainname;
                if (logging) console.log("bg clicked: sending response", bg);
                sendResponse(bg);
                if (database.clearmasterpw) {
                    masterpw = "";
                    if (logging) console.log("bg clear masterpw")
                }
            } else if (request.onload) {
                onContentPageload(request, sender, sendResponse);
                persistMetadata(sendResponse);
            }
            if (logging) console.log(Date.now(), "bg addListener returning", isMasterPw(masterpw));
        });
    });
    return true;
});
async function getMetadata(request, _sender, sendResponse) {
    if (logging) console.log("bg getMetadata", bg, request);
    let sitename = database.domains[request.domainname];
    if (sitename) {
        bg.settings = database.sites[sitename];
    } else {
        bg.settings = clone(defaultSettings);
    }
    // Domain name comes from popup, which is trusted not to spoof it
    bg.settings.domainname = pwfielddomain[request.domainname] || request.domainname;
    if (logging) console.log("bg sending metadata", pwcount, bg, database);
    sendResponse({ "masterpw": masterpw || "", "pwcount": pwcount, "bg": bg, "database": database });
}
function onContentPageload(request, sender, sendResponse) {
    if (logging) console.log("bg onContentPageLoad", request);
    activetab = sender.tab;
    bg.pwcount = request.count;
    pwcount = bg.pwcount;
    if (pwcount > 0) {
        pwfielddomain[getdomainname(sender.origin)] = getdomainname(sender.tab.url);
    }
    let origin = getdomainname(sender.origin);
    domainname = pwfielddomain[origin] || origin;
    if (logging) console.log("bg pwcount, domainname, masterpw", database, bg.pwcount, domainname, isMasterPw(masterpw));
    let sitename = database.domains[domainname];
    if (sitename) {
        bg.settings = database.sites[sitename];
    } else {
        bg.settings = clone(defaultSettings);
    }
    bg.settings.domainname = domainname;
    let readyForClick = false;
    if (masterpw && bg.settings.sitename && bg.settings.username) {
        readyForClick = true;
    }
    let sitepass = "";
    if (bg.pwcount !== 0 && bg.settings.username) {
        let p = generate(bg);
        sitepass = p;
    }
    if (logging) console.log(Date.now(), "bg send response", { cmd: "fillfields", "u": bg.settings.username || "", "p": sitepass, "readyForClick": readyForClick });
    sendResponse({ cmd: "fillfields", "u": bg.settings.username || "", "p": sitepass, "readyForClick": readyForClick });
}
async function persistMetadata(sendResponse) {
    // localStorage[name] = JSON.stringify(value);
    if (logging) console.log("bg persistMetadata", bg, database);
    chrome.storage.session.set({ "ssp": { "masterpw": masterpw } });
    let found = await getRootFolder(sendResponse);
    if (found.length > 1) return;
    let rootFolder = found[0];
    let allchildren = await chrome.bookmarks.getChildren(rootFolder.id);;
    if (bg.settings.sitename) {
        database.domains[bg.settings.domainname] = bg.settings.sitename;
        database.sites[bg.settings.sitename] = bg.settings;
    } else if (bg.settings.domainname) {
        let sitename = database.domains[bg.settings.domainname];
        delete database.domains[bg.settings.domainname];
        // Delete the item in domain.sites if there are no entries in
        // database.domains that refers to it
        if (Object.values(database.domains).includes(sitename)) {
            delete database.sites[sitename];
        }
        for (let i = 0; i < allchildren.length; i++) {
            if (allchildren[i].title === bg.settings.domainname) chrome.bookmarks.remove(allchildren[i].id, (_e) => {
                if (logging) console.log("bg removed settings bookmark", _e, allchildren[i]);
            });
        }
    }
    if (database && database.sites && database.sites[""] || database.sites["undefined"]) {
        console.log("bg bad sitename", database);
{'fraction_non_alphanumeric': 0.1322559605768534, 'fraction_numerical': 0.004275672150155808, 'mean_word_length': 2.610675039246468, 'pattern_counts': {'":': 28, '<': 5, '<?xml version=': 0, '>': 13, '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': '39140', 'n_tokens_mistral': 4092, 'n_tokens_neox': 3905, 'n_words': 950}