Compare commits

..

No commits in common. "de36d49fc05df6059e0f5c68a467c3d35b80ea8d" and "97f5a8671a920dc7aa2ffdef5070001d7c71c4c3" have entirely different histories.

3 changed files with 95 additions and 208 deletions

View File

@ -1,37 +1,15 @@
chrome.runtime.onInstalled.addListener(function() { chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({ "id": "copy-form", "title": "Copy Form", "contexts": ["editable"] }); 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.create({ "id": "paste_form", "title": "Paste Form", "contexts": ["editable"], enabled: false });
chrome.contextMenus.create({ "id": "paste-form2", "title": "Paste Form (with Hidden)", "contexts": ["editable"], enabled: false });
}); });
chrome.contextMenus.onClicked.addListener(function(msg, tab) { chrome.contextMenus.onClicked.addListener(function(msg, tab) {
switch (msg.menuItemId) { switch (msg.menuItemId) {
case "copy-form": case "copy_form":
copyFormContextMenu(tab); copyFormContextMenu(tab);
break; break;
case "paste-form": case "paste_form":
pasteFormContextMenu(tab, false); pastFormContextMenu(tab);
break;
case "paste-form2":
pasteFormContextMenu(tab, true);
break;
default: break;
}
});
chrome.commands.onCommand.addListener(function (command) {
switch (command) {
case 'copy-form':
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
copyFormContextMenu(tabs[0]);
});
break;
case 'paste-form':
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
pasteFormContextMenu(tabs[0], false);
});
break; break;
default: break; default: break;
} }
@ -42,26 +20,23 @@ chrome.commands.onCommand.addListener(function (command) {
function copyFormContextMenu(tab) { function copyFormContextMenu(tab) {
chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) { chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) {
try { if (response) {
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 });
chrome.contextMenus.update("paste-form2", { enabled: true });
}); });
} catch (err) { } else {
// Do nothing // Reload required
} }
}); });
} }
function pasteFormContextMenu(tab, includeHiddenEl) { function pastFormContextMenu(tab) {
chrome.storage.local.get('clipboard', function(response) { chrome.storage.local.get('clipboard', function(response) {
try { try {
const payload = { let msgData = JSON.parse(response.clipboard);
action: (includeHiddenEl === true ? "paste2" : "paste"), msgData.action = "paste";
formData: JSON.parse(response.clipboard)
};
chrome.tabs.sendMessage(tab.id, payload); chrome.tabs.sendMessage(tab.id, msgData);
} catch (err) { } catch (err) {
// Do nothing // Do nothing
} }

View File

@ -1,97 +1,75 @@
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
switch (message.action) {
const parentForm = getParentFormNode(document.activeElement);
if (parentForm == null) {
console.log("Form not found");
return;
}
switch (request.action) {
case "copy": case "copy":
const result = copyFormElementValues(document.activeElement); const result = buildFormValueSet(parentForm, "input", "select", "textarea");
console.log(result);
sendResponse(result); sendResponse(result);
break; break;
case "paste": case "paste":
pasteFormElementValues(document.activeElement, message.formData, false); delete request.pasteForm;
break;
case "paste2": // console.log(request);
pasteFormElementValues(document.activeElement, message.formData, true);
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<foundElements.length; x++) {
setFormElementValue(foundElements[x], request[key]);
}
}
}
break; break;
default: 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;
}
for (let key in formData) {
if (String.isNullOrWhitespace(key)) {
continue;
}
const dataType = Object.getDataType(formData[key]);
if (dataType == "object") {
continue;
}
const foundEls = container.querySelectorAll("[name='" + key + "']");
if (foundEls.length <= 0) {
continue;
}
if (dataType == "array") {
setFormElementValues(foundEls, formData[key], includeHidden);
} else {
for (let x=0; x<foundEls.length; x++) {
setFormElementValue(foundEls[x], formData[key], includeHidden);
}
}
}
writeLog("Pasted");
}
/** /**
* Get node container. * Check the data type of the value (string, number, array, object).
*/ */
function getContainer(node) { function checkDataType(value) {
let result = node; if (String.isNullOrUndefined(value)) {
return "null";
}
if (result.nodeName.toLocaleLowerCase() == "iframe") { if (typeof(value) == "object") {
writeLog("iFrame found"); if (Array.isArray(value)) {
return "array";
result = getIFrameFormNode(document.activeElement);
} else {
result = getParentFormNode(document.activeElement);
if (result == null) {
writeLog("No form found");
result = node.parentNode;
} else { } else {
writeLog("Form found"); return "object";
} }
} }
return ((typeof(result) == "undefined") ? null : result); return typeof(value);
} }
/** /**
@ -102,7 +80,10 @@ function getParentFormNode(node) {
// Special case, look inside iframe. // Special case, look inside iframe.
if (result.nodeName.toLocaleLowerCase() == "iframe") { if (result.nodeName.toLocaleLowerCase() == "iframe") {
return null; const foundForms = (result.contentDocument || result.contentWindow).getElementsByTagName("form");
if (foundForms.length > 0) {
return foundForms[0];
}
} }
while (true) { while (true) {
@ -122,25 +103,9 @@ function getParentFormNode(node) {
} }
/** /**
* Get form node in iframe. * Push form element value to array.
*/ */
function getIFrameFormNode(node) { function buildFormValueSet(form, ...tagNames){
if (node.nodeName.toLocaleLowerCase() != "iframe") {
return null;
}
const foundEls = (node.contentDocument || node.contentWindow).getElementsByTagName("form");
if (foundEls.length <= 0) {
return null;
}
return foundEls[0];
}
/**
* Get form element values.
*/
function getFormElementValueSet(form, ...tagNames){
let result = { }; let result = { };
for (let x=0; x<tagNames.length; x++) { for (let x=0; x<tagNames.length; x++) {
@ -149,20 +114,17 @@ function getFormElementValueSet(form, ...tagNames){
for (let i=0; i<els.length; i++) { for (let i=0; i<els.length; i++) {
let newValue = null; let newValue = null;
if (String.isNullOrWhitespace(els[i].name)) {
continue;
}
switch (els[i].type) switch (els[i].type)
{ {
case "hidden":
// Do nothing
break;
case "checkbox": case "checkbox":
newValue = els[i].checked;
break;
case "radio": case "radio":
newValue = els[i].checked; newValue = els[i].checked;
break; break;
// case "hidden":
// continue;
case "submit":
continue;
default: default:
newValue = els[i].value; newValue = els[i].value;
break; break;
@ -174,7 +136,7 @@ function getFormElementValueSet(form, ...tagNames){
if (result[els[i].name]) { if (result[els[i].name]) {
// Convert single to array // Convert single to array
if (Object.getDataType(result[els[i].name]) != "array") { if (checkDataType(result[els[i].name]) != "array") {
result[els[i].name] = [result[els[i].name]]; result[els[i].name] = [result[els[i].name]];
} }
@ -191,20 +153,14 @@ function getFormElementValueSet(form, ...tagNames){
/** /**
* Set single form element value. * Set single form element value.
*/ */
function setFormElementValue(el, value, includeHidden) { function setFormElementValue(el, value) {
switch (el.type) { switch (el.type) {
case "hidden": case "hidden":
if (includeHidden === true) {
el.value = value;
}
break; break;
case "checkbox": case "checkbox":
case "radio": case "radio":
el.checked = value; el.checked = value;
break; break;
case "submit":
break;
default: default:
el.value = value; el.value = value;
break; break;
@ -214,38 +170,12 @@ function setFormElementValue(el, value, includeHidden) {
/** /**
* Set form element value from an array of values. * Set form element value from an array of values.
*/ */
function setFormElementValues(els, values, includeHidden) { function setFormElementValues(els, values) {
for (let i=0; i<Math.min(els.length, values.length); i++) { for (let i=0; i<Math.min(els.length, values.length); i++) {
setFormElementValue(els[i], values[i], includeHidden); setFormElementValue(els[i], values[i]);
} }
} }
/**
* Write log to console.
*/
function writeLog(message) {
console.log("Form Copypasta > " + message);
}
Object.getDataType = function(value) {
if (String.isNullOrUndefined(value)) {
return "null";
}
if (typeof(value) == "object") {
if (Array.isArray(value)) {
return "array";
} else {
return "object";
}
}
return typeof(value);
};
String.isNullOrUndefined = function(value) { String.isNullOrUndefined = function(value) {
if (typeof (value) == "undefined") { if (typeof (value) == "undefined") {
return true; return true;

View File

@ -2,20 +2,7 @@
"manifest_version": 3, "manifest_version": 3,
"name": "Form Copypasta", "name": "Form Copypasta",
"description": "Copy and paste form element values. Not for distribution. Internal use only. Based on Copy Form (0.0.1.2) by Sam Larison.", "description": "Copy and paste form element values. Not for distribution. Internal use only. Based on Copy Form (0.0.1.2) by Sam Larison.",
"version": "0.1.0.075", "version": "0.1.0.028",
"icons": {
"16": "icon16.png",
"32": "icon32.png",
"48": "icon48.png",
"64": "icon64.png",
"128": "icon128.png"
},
"permissions": [
"activeTab",
"contextMenus",
"storage",
"tabs"
],
"content_scripts": [ "content_scripts": [
{ {
"js": [ "js": [
@ -32,22 +19,17 @@
"service_worker": "background.js", "service_worker": "background.js",
"type": "module" "type": "module"
}, },
"commands": { "icons": {
"copy-form": { "16": "icon16.png",
"suggested_key": { "32": "icon32.png",
"default": "Ctrl+Shift+2", "48": "icon48.png",
"windows": "Ctrl+Shift+2", "64": "icon64.png",
"mac": "Command+Shift+2" "128": "icon128.png"
}, },
"description": "Copy form" "permissions": [
}, "activeTab",
"paste-form": { "contextMenus",
"suggested_key": { "storage",
"default": "Ctrl+Shift+3", "tabs"
"windows": "Ctrl+Shift+3", ]
"mac": "Command+Shift+3"
},
"description": "Paste form"
}
}
} }