commit ec4daf2268cc6f69a703b522b202f1fc1b624b76 Author: Ray Date: Thu Feb 1 00:53:14 2024 +0000 Initial commit diff --git a/background.js b/background.js new file mode 100644 index 0000000..73ccbf7 --- /dev/null +++ b/background.js @@ -0,0 +1,44 @@ +chrome.runtime.onInstalled.addListener(function() { + chrome.contextMenus.create({ "id": "copy_form", "title": "Copy Form", "contexts": ["editable"] }); + chrome.contextMenus.create({ "id": "paste_form", "title": "Paste Form", "contexts": ["editable"], enabled: false }); +}); + +chrome.contextMenus.onClicked.addListener(function(msg, tab) { + switch (msg.menuItemId) { + case "copy_form": + copyFormContextMenu(tab); + break; + case "paste_form": + pastFormContextMenu(tab); + break; + default: break; + } +}); + + + + +function copyFormContextMenu(tab) { + chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) { + if (response) { + chrome.storage.local.set({ 'clipboard': JSON.stringify(response) },function() { + chrome.contextMenus.update("paste_form", { enabled: true }); + }); + } else { + // Reload required + } + }); +} + +function pastFormContextMenu(tab) { + chrome.storage.local.get('clipboard', function(response) { + try { + let msgData = JSON.parse(response.clipboard); + msgData.action = "paste"; + + chrome.tabs.sendMessage(tab.id, msgData); + } catch (err) { + // Do nothing + } + }); +} \ No newline at end of file diff --git a/content.js b/content.js new file mode 100644 index 0000000..55662a7 --- /dev/null +++ b/content.js @@ -0,0 +1,203 @@ +chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + + const parentForm = getParentFormNode(document.activeElement); + if (parentForm == null) { + console.log("Form not found"); + return; + } + + switch (request.action) { + case "copy": + const result = buildFormValueSet(parentForm, "input", "select", "textarea"); + + console.log(result); + + sendResponse(result); + + break; + case "paste": + delete request.pasteForm; + + // console.log(request); + + for (let key in request) { + if (String.isNullOrWhitespace(key)) { + continue; + } + + const dataType = checkDataType(request[key]); + if (dataType == "object") { + continue; + } + + const foundElements = parentForm.querySelectorAll("[name='" + key + "']"); + if (foundElements.length <= 0) { + continue; + } + + if (dataType == "array") { + setFormElementValues(foundElements, request[key]); + } else { + for (let x=0; x 0) { + return foundForms[0]; + } + } + + while (true) { + if (String.isNullOrUndefined(result)) { + result = null; + break; + } + + if (result.nodeName.toLocaleLowerCase() == "form") { + break; + } + + result = result.parentNode; + } + + return result; +} + +/** + * Push form element value to array. + */ +function buildFormValueSet(form, ...tagNames){ + let result = { }; + + for (let x=0; x" + ] + } + ], + "background": { + "service_worker": "background.js", + "type": "module" + }, + "icons": { + "16": "icon16.png", + "32": "icon32.png", + "48": "icon48.png", + "64": "icon64.png", + "128": "icon128.png" + }, + "permissions": [ + "activeTab", + "contextMenus", + "storage", + "tabs" + ] +} \ No newline at end of file