Refactor to separate form-data fro message action

This commit is contained in:
Ray 2024-02-01 22:03:49 +00:00
parent 2b97d44a4f
commit a39229690c
2 changed files with 66 additions and 49 deletions

View File

@ -24,12 +24,12 @@ chrome.contextMenus.onClicked.addListener(function(msg, tab) {
function copyFormContextMenu(tab) { function copyFormContextMenu(tab) {
chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) { chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) {
if (response) { try {
chrome.storage.local.set({ 'clipboard': JSON.stringify(response) },function() { chrome.storage.local.set({ 'clipboard': JSON.stringify(response) },function() {
chrome.contextMenus.update("paste_form", { enabled: true }); chrome.contextMenus.update("paste_form", { enabled: true });
}); });
} else { } catch (err) {
// Reload required // Do nothing
} }
}); });
} }
@ -37,10 +37,12 @@ function copyFormContextMenu(tab) {
function pasteFormContextMenu(tab, includeHiddenEl) { function pasteFormContextMenu(tab, includeHiddenEl) {
chrome.storage.local.get('clipboard', function(response) { chrome.storage.local.get('clipboard', function(response) {
try { try {
let msgData = JSON.parse(response.clipboard); const payload = {
msgData.action = (includeHiddenEl === true ? "paste2" : "paste"); action: (includeHiddenEl === true ? "paste2" : "paste"),
formData: JSON.parse(response.clipboard)
};
chrome.tabs.sendMessage(tab.id, msgData); chrome.tabs.sendMessage(tab.id, payload);
} catch (err) { } catch (err) {
// Do nothing // Do nothing
} }

View File

@ -1,56 +1,71 @@
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
let formContainer = getContainer(document.activeElement); switch (request.action) {
if (formContainer == null) { case "copy":
const result = copyFormElementValues(document.activeElement);
sendResponse(result);
break;
case "paste":
pasteFormElementValues(document.activeElement, request.formData, false);
break;
case "paste2":
pasteFormElementValues(document.activeElement, request.formData, true);
break;
default: break;
}
});
function copyFormElementValues(el) {
const container = getContainer(el);
if (container == null) {
writeLog("Form container not found");
return null;
}
const result = getFormElementValueSet(container, "input", "select", "textarea");
console.log(result);
return result;
}
function pasteFormElementValues(el, formData, includeHidden) {
if (String.isNullOrUndefined(formData)) {
return;
}
let container = getContainer(el);
if (container == null) {
writeLog("Form container not found"); writeLog("Form container not found");
return; return;
} }
switch (request.action) { for (let key in formData) {
case "copy": if (String.isNullOrWhitespace(key)) {
const result = getFormElementValueSet(formContainer, "input", "select", "textarea"); continue;
}
console.log(result); const dataType = Object.getDataType(formData[key]);
if (dataType == "object") {
continue;
}
sendResponse(result); const foundEls = container.querySelectorAll("[name='" + key + "']");
if (foundEls.length <= 0) {
continue;
}
break; if (dataType == "array") {
case "paste": setFormElementValues(foundEls, formData[key], includeHidden);
case "paste2": } else {
const includeHidden = (request.action == "paste2"); for (let x=0; x<foundEls.length; x++) {
setFormElementValue(foundEls[x], formData[key], includeHidden);
delete request.action;
for (let key in request) {
if (String.isNullOrWhitespace(key)) {
continue;
}
const dataType = Object.getDataType(request[key]);
if (dataType == "object") {
continue;
}
const foundEls = formContainer.querySelectorAll("[name='" + key + "']");
if (foundEls.length <= 0) {
continue;
}
if (dataType == "array") {
setFormElementValues(foundEls, request[key], includeHidden);
} else {
for (let x=0; x<foundEls.length; x++) {
setFormElementValue(foundEls[x], request[key], includeHidden);
}
}
} }
}
break;
default: break;
} }
}
});
/** /**