2024-02-01 00:53:14 +00:00
|
|
|
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
2024-02-01 20:31:47 +00:00
|
|
|
let formContainer = getContainer(document.activeElement);
|
|
|
|
if (formContainer == null) {
|
|
|
|
writeLog("Form container not found");
|
2024-02-01 00:53:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (request.action) {
|
|
|
|
case "copy":
|
2024-02-01 20:31:47 +00:00
|
|
|
const result = getFormElementValueSet(formContainer, "input", "select", "textarea");
|
2024-02-01 00:53:14 +00:00
|
|
|
|
|
|
|
console.log(result);
|
|
|
|
|
|
|
|
sendResponse(result);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "paste":
|
2024-02-01 20:31:47 +00:00
|
|
|
case "paste2":
|
|
|
|
const includeHidden = (request.action == "paste2");
|
2024-02-01 00:53:14 +00:00
|
|
|
|
2024-02-01 20:31:47 +00:00
|
|
|
delete request.action;
|
2024-02-01 00:53:14 +00:00
|
|
|
|
|
|
|
for (let key in request) {
|
|
|
|
if (String.isNullOrWhitespace(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-02-01 20:31:47 +00:00
|
|
|
const dataType = Object.getDataType(request[key]);
|
2024-02-01 00:53:14 +00:00
|
|
|
if (dataType == "object") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-02-01 20:31:47 +00:00
|
|
|
const foundEls = formContainer.querySelectorAll("[name='" + key + "']");
|
|
|
|
if (foundEls.length <= 0) {
|
2024-02-01 00:53:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataType == "array") {
|
2024-02-01 20:31:47 +00:00
|
|
|
setFormElementValues(foundEls, request[key], includeHidden);
|
2024-02-01 00:53:14 +00:00
|
|
|
} else {
|
2024-02-01 20:31:47 +00:00
|
|
|
for (let x=0; x<foundEls.length; x++) {
|
|
|
|
setFormElementValue(foundEls[x], request[key], includeHidden);
|
2024-02-01 00:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-01 20:31:47 +00:00
|
|
|
* Get node container.
|
2024-02-01 00:53:14 +00:00
|
|
|
*/
|
2024-02-01 20:31:47 +00:00
|
|
|
function getContainer(node) {
|
|
|
|
let result = node;
|
2024-02-01 00:53:14 +00:00
|
|
|
|
2024-02-01 20:31:47 +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 {
|
2024-02-01 20:31:47 +00:00
|
|
|
writeLog("Form found");
|
2024-02-01 00:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 20:31:47 +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") {
|
2024-02-01 20:31:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-01 20:31:47 +00:00
|
|
|
* Get form node in iframe.
|
2024-02-01 00:53:14 +00:00
|
|
|
*/
|
2024-02-01 20:31:47 +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;
|
2024-02-01 20:31:47 +00:00
|
|
|
// 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
|
2024-02-01 20:31:47 +00:00
|
|
|
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.
|
|
|
|
*/
|
2024-02-01 20:31:47 +00:00
|
|
|
function setFormElementValue(el, value, includeHidden) {
|
2024-02-01 00:53:14 +00:00
|
|
|
switch (el.type) {
|
|
|
|
case "hidden":
|
2024-02-01 20:31:47 +00:00
|
|
|
if (includeHidden === true) {
|
|
|
|
el.value = value;
|
|
|
|
}
|
|
|
|
|
2024-02-01 00:53:14 +00:00
|
|
|
break;
|
|
|
|
case "checkbox":
|
|
|
|
case "radio":
|
|
|
|
el.checked = value;
|
|
|
|
break;
|
2024-02-01 20:31:47 +00:00
|
|
|
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.
|
|
|
|
*/
|
2024-02-01 20:31:47 +00:00
|
|
|
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++) {
|
2024-02-01 20:31:47 +00:00
|
|
|
setFormElementValue(els[i], values[i], includeHidden);
|
2024-02-01 00:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 20:31:47 +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);
|
|
|
|
}
|
|
|
|
};
|