release/0.1.0.035 #1
@ -1,6 +1,7 @@
|
|||||||
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) {
|
||||||
@ -9,7 +10,10 @@ chrome.contextMenus.onClicked.addListener(function(msg, tab) {
|
|||||||
copyFormContextMenu(tab);
|
copyFormContextMenu(tab);
|
||||||
break;
|
break;
|
||||||
case "paste_form":
|
case "paste_form":
|
||||||
pastFormContextMenu(tab);
|
pasteFormContextMenu(tab, false);
|
||||||
|
break;
|
||||||
|
case "paste_form2":
|
||||||
|
pasteFormContextMenu(tab, true);
|
||||||
break;
|
break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
@ -30,11 +34,11 @@ function copyFormContextMenu(tab) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function pastFormContextMenu(tab) {
|
function pasteFormContextMenu(tab, includeHiddenEl) {
|
||||||
chrome.storage.local.get('clipboard', function(response) {
|
chrome.storage.local.get('clipboard', function(response) {
|
||||||
try {
|
try {
|
||||||
let msgData = JSON.parse(response.clipboard);
|
let msgData = JSON.parse(response.clipboard);
|
||||||
msgData.action = "paste";
|
msgData.action = (includeHiddenEl === true ? "paste2" : "paste");
|
||||||
|
|
||||||
chrome.tabs.sendMessage(tab.id, msgData);
|
chrome.tabs.sendMessage(tab.id, msgData);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
125
content.js
125
content.js
@ -1,14 +1,13 @@
|
|||||||
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
||||||
|
let formContainer = getContainer(document.activeElement);
|
||||||
const parentForm = getParentFormNode(document.activeElement);
|
if (formContainer == null) {
|
||||||
if (parentForm == null) {
|
writeLog("Form container not found");
|
||||||
console.log("Form not found");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (request.action) {
|
switch (request.action) {
|
||||||
case "copy":
|
case "copy":
|
||||||
const result = buildFormValueSet(parentForm, "input", "select", "textarea");
|
const result = getFormElementValueSet(formContainer, "input", "select", "textarea");
|
||||||
|
|
||||||
console.log(result);
|
console.log(result);
|
||||||
|
|
||||||
@ -16,30 +15,31 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case "paste":
|
case "paste":
|
||||||
delete request.pasteForm;
|
case "paste2":
|
||||||
|
const includeHidden = (request.action == "paste2");
|
||||||
|
|
||||||
// console.log(request);
|
delete request.action;
|
||||||
|
|
||||||
for (let key in request) {
|
for (let key in request) {
|
||||||
if (String.isNullOrWhitespace(key)) {
|
if (String.isNullOrWhitespace(key)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataType = checkDataType(request[key]);
|
const dataType = Object.getDataType(request[key]);
|
||||||
if (dataType == "object") {
|
if (dataType == "object") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const foundElements = parentForm.querySelectorAll("[name='" + key + "']");
|
const foundEls = formContainer.querySelectorAll("[name='" + key + "']");
|
||||||
if (foundElements.length <= 0) {
|
if (foundEls.length <= 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataType == "array") {
|
if (dataType == "array") {
|
||||||
setFormElementValues(foundElements, request[key]);
|
setFormElementValues(foundEls, request[key], includeHidden);
|
||||||
} else {
|
} else {
|
||||||
for (let x=0; x<foundElements.length; x++) {
|
for (let x=0; x<foundEls.length; x++) {
|
||||||
setFormElementValue(foundElements[x], request[key]);
|
setFormElementValue(foundEls[x], request[key], includeHidden);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,22 +54,27 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check the data type of the value (string, number, array, object).
|
* Get node container.
|
||||||
*/
|
*/
|
||||||
function checkDataType(value) {
|
function getContainer(node) {
|
||||||
if (String.isNullOrUndefined(value)) {
|
let result = node;
|
||||||
return "null";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof(value) == "object") {
|
if (result.nodeName.toLocaleLowerCase() == "iframe") {
|
||||||
if (Array.isArray(value)) {
|
writeLog("iFrame found");
|
||||||
return "array";
|
|
||||||
|
result = getIFrameFormNode(document.activeElement);
|
||||||
|
} else {
|
||||||
|
result = getParentFormNode(document.activeElement);
|
||||||
|
if (result == null) {
|
||||||
|
writeLog("No form found");
|
||||||
|
|
||||||
|
result = node.parentNode;
|
||||||
} else {
|
} else {
|
||||||
return "object";
|
writeLog("Form found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return typeof(value);
|
return ((typeof(result) == "undefined") ? null : result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,10 +85,7 @@ function getParentFormNode(node) {
|
|||||||
|
|
||||||
// Special case, look inside iframe.
|
// Special case, look inside iframe.
|
||||||
if (result.nodeName.toLocaleLowerCase() == "iframe") {
|
if (result.nodeName.toLocaleLowerCase() == "iframe") {
|
||||||
const foundForms = (result.contentDocument || result.contentWindow).getElementsByTagName("form");
|
return null;
|
||||||
if (foundForms.length > 0) {
|
|
||||||
return foundForms[0];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -103,9 +105,25 @@ function getParentFormNode(node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Push form element value to array.
|
* Get form node in iframe.
|
||||||
*/
|
*/
|
||||||
function buildFormValueSet(form, ...tagNames){
|
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 = { };
|
let result = { };
|
||||||
|
|
||||||
for (let x=0; x<tagNames.length; x++) {
|
for (let x=0; x<tagNames.length; x++) {
|
||||||
@ -116,15 +134,14 @@ function buildFormValueSet(form, ...tagNames){
|
|||||||
|
|
||||||
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;
|
||||||
@ -136,7 +153,7 @@ function buildFormValueSet(form, ...tagNames){
|
|||||||
|
|
||||||
if (result[els[i].name]) {
|
if (result[els[i].name]) {
|
||||||
// Convert single to array
|
// Convert single to array
|
||||||
if (checkDataType(result[els[i].name]) != "array") {
|
if (Object.getDataType(result[els[i].name]) != "array") {
|
||||||
result[els[i].name] = [result[els[i].name]];
|
result[els[i].name] = [result[els[i].name]];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,14 +170,20 @@ function buildFormValueSet(form, ...tagNames){
|
|||||||
/**
|
/**
|
||||||
* Set single form element value.
|
* Set single form element value.
|
||||||
*/
|
*/
|
||||||
function setFormElementValue(el, value) {
|
function setFormElementValue(el, value, includeHidden) {
|
||||||
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;
|
||||||
@ -170,12 +193,38 @@ function setFormElementValue(el, value) {
|
|||||||
/**
|
/**
|
||||||
* Set form element value from an array of values.
|
* Set form element value from an array of values.
|
||||||
*/
|
*/
|
||||||
function setFormElementValues(els, values) {
|
function setFormElementValues(els, values, includeHidden) {
|
||||||
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]);
|
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) {
|
String.isNullOrUndefined = function(value) {
|
||||||
if (typeof (value) == "undefined") {
|
if (typeof (value) == "undefined") {
|
||||||
return true;
|
return true;
|
||||||
|
@ -2,7 +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.028",
|
"version": "0.1.0.035",
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
"js": [
|
"js": [
|
||||||
|
Loading…
Reference in New Issue
Block a user