Skip to content

Commit

Permalink
Some code maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
heavensrevenge committed Jan 10, 2024
1 parent 60d80fe commit 8e6851b
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 149 deletions.
2 changes: 1 addition & 1 deletion css/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ select.input {

#resetToDefaultprofiles {
font-weight: bold;
margin: 50px 0 50px 165px;
margin: 50px 0 50px 0px;
}

#expirePasswordMinutes {
Expand Down
6 changes: 3 additions & 3 deletions html/options.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta charset="utf-8" name="viewport" content="width=device-width">
<title>PasswordMaker.org</title>
<link rel="stylesheet" href="../css/awesome-buttons.css">
<link rel="stylesheet" href="../css/options.css">
Expand Down Expand Up @@ -328,7 +328,7 @@ <h2>Settings</h2>
</div>
<div class="row">
<div class="subrow">
<button class="button red" id="resetToDefaultprofiles">Reset Profiles to Extension Defaults</button>
<button class="button red" id="resetToDefaultprofiles">Delete Synced & Local Profiles and Reset to Defaults?</button>
<label class="checkbox" for="resetToDefaultprofiles"></label>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions html/popup.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta charset="utf-8" name="viewport" content="width=device-width">
<title>PasswordMaker.org</title>
<link rel="stylesheet" href="../css/awesome-buttons.css">
<link rel="stylesheet" href="../css/popup.css">
Expand Down
129 changes: 59 additions & 70 deletions javascript/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ function addProfile() {
var p = Object.create(Profile);
p.title = "No name";
Settings.addProfile(p);
updateProfileList().then(() => {
setCurrentProfile(p);
})
updateProfileList()
.then(() => setCurrentProfile(p));
}

function removeProfile() {
Expand All @@ -36,12 +35,12 @@ function removeProfile() {
}

function removeAllProfiles() {
if (confirm("Really delete ALL local profile customizations and reset to the default profiles?")) {
chrome.storage.local.remove("profiles").then(() => {
Settings.loadProfiles().then(() => {
updateProfileList();
});
})
if (confirm("Really delete ALL local AND synced profile customizations and reset to the default profiles?")) {
chrome.storage.local.remove(["profiles", "synced_profiles"])
.then(() => clearSyncData())
.then(() => document.getElementById("syncProfiles").checked = false)
.then(() => Settings.loadProfiles())
.then(() => updateProfileList());
}
}

Expand Down Expand Up @@ -164,18 +163,17 @@ function showOptions() {
}
});

chrome.storage.local.get(["storeLocation", "expire_password_minutes", "master_password_hash"]).then((result) => {
document.getElementById("store_location").value = result["storeLocation"];
document.getElementById("expirePasswordMinutes").value = (result["expire_password_minutes"] || 5);
updateStyle(document.getElementById("password_expire_row"), "hidden", (result["storeLocation"] !== "memory_expire"));
updateStyle(document.getElementById("master_password_row"), "hidden", (result["master_password_hash"] === undefined));
document.getElementById("masterPassword").setAttribute("placeholder", "Encrypted In Storage");
showSection("general_settings");
}).then(() => {
updateSyncStatus().then(() => {
filterProfiles();
});
});
chrome.storage.local.get(["storeLocation", "expire_password_minutes", "master_password_hash"])
.then((result) => {
document.getElementById("store_location").value = result["storeLocation"];
document.getElementById("expirePasswordMinutes").value = (result["expire_password_minutes"] || 5);
updateStyle(document.getElementById("password_expire_row"), "hidden", (result["storeLocation"] !== "memory_expire"));
updateStyle(document.getElementById("master_password_row"), "hidden", (result["master_password_hash"] === undefined));
document.getElementById("masterPassword").setAttribute("placeholder", "Encrypted In Storage");
showSection("general_settings");
})
.then(() => updateSyncStatus())
.then(() => filterProfiles());
}

function showInformation() {
Expand All @@ -185,9 +183,10 @@ function showInformation() {
function showSection(showId) {
document.getElementById("checkStrength").checked = false;
showStrengthSection();
Array.from(document.querySelectorAll("section")).concat(Array.from(document.querySelectorAll("aside"))).filter((el) => {
return el.id !== showId;
}).forEach((el) => el.style.display = "none");
Array.from(document.querySelectorAll("section"))
.concat(Array.from(document.querySelectorAll("aside")))
.filter((el) => el.id !== showId)
.forEach((el) => el.style.display = "none");
document.getElementById(showId).style.display = "block";
}

Expand All @@ -198,10 +197,9 @@ function highlightProfile() {

function updateStorageLocation() {
var storeLocation = document.getElementById("store_location").value;
chrome.storage.local.set({ "storeLocation": storeLocation }).then(() => {
Settings.setStoreLocation(storeLocation);
updateStyle(document.getElementById("password_expire_row"), "hidden", (storeLocation !== "memory_expire"));
});
chrome.storage.local.set({ "storeLocation": storeLocation })
.then(() => Settings.setStoreLocation(storeLocation))
.then(() => updateStyle(document.getElementById("password_expire_row"), "hidden", (storeLocation !== "memory_expire")));
}

function saveProfile() {
Expand Down Expand Up @@ -236,26 +234,25 @@ function saveProfile() {
selected.selectedCharset = document.getElementById("charset").value;
}

Settings.saveProfiles();
updateProfileList().then(() => {
setCurrentProfile(selected);
highlightProfile();
oldHashWarning(selected.hashAlgorithm);
});
Settings.saveProfiles()
.then(() => updateProfileList())
.then(() => {
setCurrentProfile(selected);
highlightProfile();
oldHashWarning(selected.hashAlgorithm);
});
}

function cloneProfile() {
var p = Object.assign(Object.create(Profile), Settings.getProfile(Settings.currentProfile));
p.title = p.title + " Copy";
Settings.addProfile(p);
updateProfileList().then(() => {
setCurrentProfile(p);
});
updateProfileList().then(() => setCurrentProfile(p));
}

function editProfile(event) {
if (event.target.classList.contains("link")) {
var targetId = event.target.id.slice(-1);
var targetId = event.target.id.replace(/^\D+/, "");
setCurrentProfile(Settings.getProfile(targetId));
}
}
Expand All @@ -272,13 +269,13 @@ function updateProfileList() {
}

function syncSucccess(syncPassHash) {
Settings.saveSyncedProfiles(syncPassHash, Settings.encrypt(syncPassHash, JSON.stringify(Settings.profiles)));
chrome.storage.local.set({ "sync_profiles": true, "sync_profiles_password": syncPassHash }).then(() => {
updateProfileList().then(() => {
return Settings.saveSyncedProfiles(syncPassHash, Settings.encrypt(syncPassHash, JSON.stringify(Settings.profiles)))
.then(() => chrome.storage.local.set({ "sync_profiles": true, "sync_profiles_password": syncPassHash }))
.then(() => updateProfileList())
.then(() => {
document.getElementById("syncProfilesPassword").value = "";
updateSyncStatus();
});
});
}

function setSyncPassword() {
Expand All @@ -288,7 +285,7 @@ function setSyncPassword() {
return;
}

chrome.storage.local.get(["syncDataAvailable", "synced_profiles"]).then((result1) => {
return chrome.storage.local.get(["syncDataAvailable", "synced_profiles"]).then((result1) => {
var syncPassHash = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(syncPassValue));

if (result1["syncDataAvailable"] === true) {
Expand All @@ -308,15 +305,11 @@ function setSyncPassword() {
}

function clearSyncData() {
chrome.storage.sync.clear().then(() => {
chrome.storage.local.remove(["syncDataAvailable", "sync_profiles", "synced_profiles", "synced_profiles_keys", "sync_profiles_password"]).then(() => {
Settings.loadProfiles().then(() => {
updateProfileList().then(() => {
updateSyncStatus();
});
});
});
});
chrome.storage.sync.clear()
.then(() => chrome.storage.local.remove(["syncDataAvailable", "sync_profiles", "synced_profiles", "synced_profiles_keys", "sync_profiles_password"]))
.then(() => Settings.loadProfiles())
.then(() => updateProfileList())
.then(() => updateSyncStatus());
}

function updateSyncStatus() {
Expand All @@ -341,9 +334,7 @@ function updateSyncStatus() {
});
} else {
return chrome.storage.local.remove(["sync_profiles", "sync_profiles_password"]).then(() => {
Settings.loadProfiles().then(() => {
updateProfileList();
});
return Settings.loadProfiles().then(() => updateProfileList());
});
}
}
Expand Down Expand Up @@ -394,11 +385,9 @@ function updateAlphaSortProfiles() {
} else {
chrome.storage.local.remove("alpha_sort_profiles");
}
Settings.loadProfiles().then(() => {
updateProfileList().then(() => {
filterProfiles();
});
});
Settings.loadProfiles()
.then(() => updateProfileList())
.then(() => filterProfiles());
}

function sanitizePasswordLength() {
Expand All @@ -417,7 +406,7 @@ function sanitizeExpireTime(newExpireTime) {
newExpireTime = 720;
field.value = "720";
}
newExpireTime = parseInt(newExpireTime, 10);
newExpireTime = parseInt(newExpireTime);
field.value = newExpireTime;
return newExpireTime;
}
Expand Down Expand Up @@ -484,9 +473,11 @@ function filterProfiles() {

// Loop through all list items, and hide those which don't match the search query
for (var i = 0; i < list.length; i++) {
var itemId = list[i].getElementsByTagName("span")[0].id.slice(-1);
var itemId = list[i].getElementsByTagName("span")[0].id.replace(/^\D+/, "");
var prof = Settings.getProfile(itemId);
if (prof.title.toUpperCase().includes(filter) || prof.strUseText.toUpperCase().includes(filter) || prof.username.toUpperCase().includes(filter) || prof.description.toUpperCase().includes(filter) || prof.siteList.toUpperCase().includes(filter)) {
if (prof.title.toUpperCase().includes(filter) || prof.strUseText.toUpperCase().includes(filter) ||
prof.username.toUpperCase().includes(filter) || prof.description.toUpperCase().includes(filter) ||
prof.siteList.toUpperCase().includes(filter)) {
list[i].style.display = "";
} else {
list[i].style.display = "none";
Expand Down Expand Up @@ -538,13 +529,11 @@ document.addEventListener("DOMContentLoaded", () => {
if (result["storeLocation"] === undefined) {
chrome.storage.local.set({ "storeLocation": "memory" });
}
Settings.migrateFromStorage();

Settings.loadProfiles().then(() => {
updateProfileList().then(() => {
setCurrentProfile(Settings.profiles[0]);
});

Settings.migrateFromStorage()
.then(() => Settings.loadProfiles())
.then(() => updateProfileList())
.then(() => setCurrentProfile(Settings.profiles[0]))
.then(() => {
chrome.storage.local.get(["hide_generated_password", "sync_profiles", "master_password_hash", "use_verification_code", "show_password_strength", "alpha_sort_profiles"]).then((result) => {
document.getElementById("hidePassword").checked = result["hide_generated_password"];
document.getElementById("keepMasterPasswordHash").checked = result["master_password_hash"];
Expand Down
58 changes: 28 additions & 30 deletions javascript/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function updateFields() {
var confirmationEl = document.getElementById("confirmation");
var passStrength = 0;

chrome.storage.local.get(["master_password_hash", "show_password_strength", "storeLocation", "use_verification_code"]).then((result) => {
return chrome.storage.local.get(["master_password_hash", "show_password_strength", "storeLocation", "use_verification_code"]).then((result) => {
if (passwordEl.value.length === 0) {
document.getElementById("generated").value = "Please Enter Password";
setPasswordColors("#000000", "#85FFAB");
Expand Down Expand Up @@ -136,7 +136,7 @@ function showButtonsScript() {
function showButtons() {
document.getElementById("copypassword").classList.remove("hidden");
// Don't run executeScript() on built-in chrome://, opera:// or about:// browser pages since it isn't allowed anyway
// Also cant run on the Chrome Web Store/Extension Gallery
// Also cant run on the Chrome Web Store/Extension Gallery or extension options pages
if (!(/^about|^chrome|^edge|^opera|(chrome|chromewebstore)\.google\.com|.*extension:/i).test(Settings.currentUrl)) {
chrome.tabs.query({active: true, currentWindow: true}).then((tabs) => {
chrome.scripting.executeScript({
Expand Down Expand Up @@ -188,38 +188,37 @@ function fillFieldsScript(args) {
}

function fillFields(generatedPass) {
updateFields();
// Don't run executeScript() on built-in chrome://, opera:// or about:// browser pages since it isn't allowed anyway
// Also cant run on the Chrome Web Store/Extension Gallery
if (!(/^about|^chrome|^edge|^opera|(chrome|chromewebstore)\.google\.com|.*extension:/i).test(Settings.currentUrl)) {
chrome.tabs.query({active: true, currentWindow: true}).then((tabs) => {
chrome.scripting.executeScript({
target: {tabId: tabs[0].id, allFrames: true},
args : [ generatedPass ],
func: fillFieldsScript,
}).then(() => {
window.close();
}).catch((err) => {
console.log("Fill field error: " + err.message);
updateFields().then(() => {
// Don't run executeScript() on built-in chrome://, opera:// or about:// browser pages since it isn't allowed anyway
// Also cant run on the Chrome Web Store/Extension Gallery
if (!(/^about|^chrome|^edge|^opera|(chrome|chromewebstore)\.google\.com|.*extension:/i).test(Settings.currentUrl)) {
chrome.tabs.query({active: true, currentWindow: true}).then((tabs) => {
chrome.scripting.executeScript({
target: {tabId: tabs[0].id, allFrames: true},
args : [ generatedPass ],
func: fillFieldsScript,
}).then(() => {
window.close();
}).catch((err) => {
console.log("Fill field error: " + err.message);
});
});
});
}
}
});
}

function copyPassword() {
updateFields();
chrome.tabs.query({
"windowType": "popup"
}).then(() => {
navigator.clipboard.writeText(document.getElementById("generated").value).then(() => {
window.close();
updateFields().then(() => {
chrome.tabs.query({
"windowType": "popup"
}).then(() => {
navigator.clipboard.writeText(document.getElementById("generated").value).then(() => window.close());
});
});
}

function openOptions() {
chrome.runtime.openOptionsPage();
window.close();
chrome.runtime.openOptionsPage().then(() => window.close());
}

function getVerificationCode(password) {
Expand Down Expand Up @@ -270,8 +269,7 @@ function sharedInit(decryptedPass) {
});
document.getElementById("profile").value = (getAutoProfileIdForUrl() || Settings.profiles[0].id);

updateProfileText();
updateFields();
onProfileChanged();
});
}

Expand Down Expand Up @@ -301,9 +299,9 @@ document.addEventListener("DOMContentLoaded", () => {
if (result["storeLocation"] === undefined) {
chrome.storage.local.set({ "storeLocation": "memory" });
}
Settings.migrateFromStorage();

Settings.loadProfiles().then(() => {
Settings.migrateFromStorage()
.then(() => Settings.loadProfiles())
.then(() => {
document.querySelectorAll("#password, #confirmation").forEach((el) => el.addEventListener("keyup", Settings.setPassword));
document.querySelectorAll("input").forEach((el) => el.addEventListener("input", delayedUpdate));
document.getElementById("profile").addEventListener("change", onProfileChanged);
Expand Down
Loading

0 comments on commit 8e6851b

Please sign in to comment.