form-copypasta-crx/content.js
Ray 2b97d44a4f Added support for paste with hidden elements
Added support for no parent form node found
Refactored
2024-02-01 20:31:47 +00:00

252 lines
4.5 KiB
JavaScript

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
let formContainer = getContainer(document.activeElement);
if (formContainer == null) {
writeLog("Form container not found");
return;
}
switch (request.action) {
case "copy":
const result = getFormElementValueSet(formContainer, "input", "select", "textarea");
console.log(result);
sendResponse(result);
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<foundEls.length; x++) {
setFormElementValue(foundEls[x], request[key], includeHidden);
}
}
}
break;
default: break;
}
});
/**
* Get node container.
*/
function getContainer(node) {
let result = node;
if (result.nodeName.toLocaleLowerCase() == "iframe") {
writeLog("iFrame found");
result = getIFrameFormNode(document.activeElement);
} else {
result = getParentFormNode(document.activeElement);
if (result == null) {
writeLog("No form found");
result = node.parentNode;
} else {
writeLog("Form found");
}
}
return ((typeof(result) == "undefined") ? null : result);
}
/**
* Get parent form node.
*/
function getParentFormNode(node) {
let result = node;
// Special case, look inside iframe.
if (result.nodeName.toLocaleLowerCase() == "iframe") {
return null;
}
while (true) {
if (String.isNullOrUndefined(result)) {
result = null;
break;
}
if (result.nodeName.toLocaleLowerCase() == "form") {
break;
}
result = result.parentNode;
}
return result;
}
/**
* Get form node in iframe.
*/
function getIFrameFormNode(node) {
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 = { };
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 "checkbox":
case "radio":
newValue = els[i].checked;
break;
// case "hidden":
// continue;
case "submit":
continue;
default:
newValue = els[i].value;
break;
}
if (newValue === null) {
continue;
}
if (result[els[i].name]) {
// Convert single to array
if (Object.getDataType(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, includeHidden) {
switch (el.type) {
case "hidden":
if (includeHidden === true) {
el.value = value;
}
break;
case "checkbox":
case "radio":
el.checked = value;
break;
case "submit":
break;
default:
el.value = value;
break;
}
}
/**
* Set form element value from an array of values.
*/
function setFormElementValues(els, values, includeHidden) {
for (let i=0; i<Math.min(els.length, values.length); i++) {
setFormElementValue(els[i], values[i], includeHidden);
}
}
/**
* 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) {
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);
}
};