form-copypasta-crx/content.js
2024-02-01 00:53:14 +00:00

203 lines
3.7 KiB
JavaScript

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<foundElements.length; x++) {
setFormElementValue(foundElements[x], request[key]);
}
}
}
break;
default: break;
}
});
/**
* Check the data type of the value (string, number, array, object).
*/
function checkDataType(value) {
if (String.isNullOrUndefined(value)) {
return "null";
}
if (typeof(value) == "object") {
if (Array.isArray(value)) {
return "array";
} else {
return "object";
}
}
return typeof(value);
}
/**
* Get parent form node.
*/
function getParentFormNode(node) {
let result = node;
// Special case, look inside iframe.
if (result.nodeName.toLocaleLowerCase() == "iframe") {
const foundForms = (result.contentDocument || result.contentWindow).getElementsByTagName("form");
if (foundForms.length > 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<tagNames.length; x++) {
let els = form.getElementsByTagName(tagNames[x]);
for (let i=0; i<els.length; i++) {
let newValue = null;
switch (els[i].type)
{
case "hidden":
// Do nothing
break;
case "checkbox":
newValue = els[i].checked;
break;
case "radio":
newValue = els[i].checked;
break;
default:
newValue = els[i].value;
break;
}
if (newValue === null) {
continue;
}
if (result[els[i].name]) {
// Convert single to array
if (checkDataType(result[els[i].name]) != "array") {
result[els[i].name] = [result[els[i].name]];
}
result[els[i].name].push(newValue);
} else {
result[els[i].name] = newValue;
}
}
}
return result;
}
/**
* Set single form element value.
*/
function setFormElementValue(el, value) {
switch (el.type) {
case "hidden":
break;
case "checkbox":
case "radio":
el.checked = value;
break;
default:
el.value = value;
break;
}
}
/**
* Set form element value from an array of values.
*/
function setFormElementValues(els, values) {
for (let i=0; i<Math.min(els.length, values.length); i++) {
setFormElementValue(els[i], values[i]);
}
}
String.isNullOrUndefined = function(value) {
if (typeof (value) == "undefined") {
return true;
}
if (value == null) {
return true;
}
return false;
};
String.isNullOrWhitespace = function(value) {
const a = this;
if (String.isNullOrUndefined(value)) {
return true;
}
if (typeof(value) == "string") {
return (value.trim().length <= 0);
} else {
return (value.toString().trim().length <= 0);
}
};