From a39229690cdc84ef431b019117902aae77f89cec Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 1 Feb 2024 22:03:49 +0000 Subject: [PATCH] Refactor to separate form-data fro message action --- background.js | 14 ++++--- content.js | 101 +++++++++++++++++++++++++++++--------------------- 2 files changed, 66 insertions(+), 49 deletions(-) diff --git a/background.js b/background.js index 06754e0..81f1627 100644 --- a/background.js +++ b/background.js @@ -24,12 +24,12 @@ chrome.contextMenus.onClicked.addListener(function(msg, tab) { function copyFormContextMenu(tab) { chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) { - if (response) { + try { chrome.storage.local.set({ 'clipboard': JSON.stringify(response) },function() { chrome.contextMenus.update("paste_form", { enabled: true }); }); - } else { - // Reload required + } catch (err) { + // Do nothing } }); } @@ -37,10 +37,12 @@ function copyFormContextMenu(tab) { function pasteFormContextMenu(tab, includeHiddenEl) { chrome.storage.local.get('clipboard', function(response) { try { - let msgData = JSON.parse(response.clipboard); - msgData.action = (includeHiddenEl === true ? "paste2" : "paste"); + const payload = { + action: (includeHiddenEl === true ? "paste2" : "paste"), + formData: JSON.parse(response.clipboard) + }; - chrome.tabs.sendMessage(tab.id, msgData); + chrome.tabs.sendMessage(tab.id, payload); } catch (err) { // Do nothing } diff --git a/content.js b/content.js index 3e9b97c..1eddf6c 100644 --- a/content.js +++ b/content.js @@ -1,56 +1,71 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { - let formContainer = getContainer(document.activeElement); - if (formContainer == null) { + switch (request.action) { + 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"); return; } - switch (request.action) { - case "copy": - const result = getFormElementValueSet(formContainer, "input", "select", "textarea"); + for (let key in formData) { + if (String.isNullOrWhitespace(key)) { + 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; - case "paste": - case "paste2": - const includeHidden = (request.action == "paste2"); - - 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