form-copypasta-crx/content.js

267 lines
4.9 KiB
JavaScript
Raw Normal View History

2024-02-01 00:53:14 +00:00
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.action) {
case "copy":
const result = copyFormElementValues(document.activeElement);
2024-02-01 00:53:14 +00:00
sendResponse(result);
break;
case "paste":
pasteFormElementValues(document.activeElement, request.formData, false);
break;
case "paste2":
pasteFormElementValues(document.activeElement, request.formData, true);
2024-02-01 00:53:14 +00:00
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);
}
}
}
}
2024-02-01 00:53:14 +00:00
/**
* Get node container.
2024-02-01 00:53:14 +00:00
*/
function getContainer(node) {
let result = node;
2024-02-01 00:53:14 +00:00
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;
2024-02-01 00:53:14 +00:00
} else {
writeLog("Form found");
2024-02-01 00:53:14 +00:00
}
}
return ((typeof(result) == "undefined") ? null : result);
2024-02-01 00:53:14 +00:00
}
/**
* Get parent form node.
*/
function getParentFormNode(node) {
let result = node;
// Special case, look inside iframe.
if (result.nodeName.toLocaleLowerCase() == "iframe") {
return null;
2024-02-01 00:53:14 +00:00
}
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.
2024-02-01 00:53:14 +00:00
*/
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){
2024-02-01 00:53:14 +00:00
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;
2024-02-01 00:53:14 +00:00
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") {
2024-02-01 00:53:14 +00:00
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) {
2024-02-01 00:53:14 +00:00
switch (el.type) {
case "hidden":
if (includeHidden === true) {
el.value = value;
}
2024-02-01 00:53:14 +00:00
break;
case "checkbox":
case "radio":
el.checked = value;
break;
case "submit":
break;
2024-02-01 00:53:14 +00:00
default:
el.value = value;
break;
}
}
/**
* Set form element value from an array of values.
*/
function setFormElementValues(els, values, includeHidden) {
2024-02-01 00:53:14 +00:00
for (let i=0; i<Math.min(els.length, values.length); i++) {
setFormElementValue(els[i], values[i], includeHidden);
2024-02-01 00:53:14 +00:00
}
}
/**
* 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);
};
2024-02-01 00:53:14 +00:00
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);
}
};