Refactor to separate form-data fro message action
This commit is contained in:
parent
2b97d44a4f
commit
a39229690c
@ -24,12 +24,12 @@ chrome.contextMenus.onClicked.addListener(function(msg, tab) {
|
||||
|
||||
function copyFormContextMenu(tab) {
|
||||
chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) {
|
||||
if (response) {
|
||||
try {
|
||||
chrome.storage.local.set({ 'clipboard': JSON.stringify(response) },function() {
|
||||
chrome.contextMenus.update("paste_form", { enabled: true });
|
||||
});
|
||||
} else {
|
||||
// Reload required
|
||||
} catch (err) {
|
||||
// Do nothing
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -37,10 +37,12 @@ function copyFormContextMenu(tab) {
|
||||
function pasteFormContextMenu(tab, includeHiddenEl) {
|
||||
chrome.storage.local.get('clipboard', function(response) {
|
||||
try {
|
||||
let msgData = JSON.parse(response.clipboard);
|
||||
msgData.action = (includeHiddenEl === true ? "paste2" : "paste");
|
||||
const payload = {
|
||||
action: (includeHiddenEl === true ? "paste2" : "paste"),
|
||||
formData: JSON.parse(response.clipboard)
|
||||
};
|
||||
|
||||
chrome.tabs.sendMessage(tab.id, msgData);
|
||||
chrome.tabs.sendMessage(tab.id, payload);
|
||||
} catch (err) {
|
||||
// Do nothing
|
||||
}
|
||||
|
75
content.js
75
content.js
@ -1,56 +1,71 @@
|
||||
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
||||
let formContainer = getContainer(document.activeElement);
|
||||
if (formContainer == null) {
|
||||
switch (request.action) {
|
||||
case "copy":
|
||||
const result = copyFormElementValues(document.activeElement);
|
||||
sendResponse(result);
|
||||
break;
|
||||
case "paste":
|
||||
pasteFormElementValues(document.activeElement, request.formData, false);
|
||||
break;
|
||||
case "paste2":
|
||||
pasteFormElementValues(document.activeElement, request.formData, true);
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
for (let key in formData) {
|
||||
if (String.isNullOrWhitespace(key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const dataType = Object.getDataType(request[key]);
|
||||
const dataType = Object.getDataType(formData[key]);
|
||||
if (dataType == "object") {
|
||||
continue;
|
||||
}
|
||||
|
||||
const foundEls = formContainer.querySelectorAll("[name='" + key + "']");
|
||||
const foundEls = container.querySelectorAll("[name='" + key + "']");
|
||||
if (foundEls.length <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dataType == "array") {
|
||||
setFormElementValues(foundEls, request[key], includeHidden);
|
||||
setFormElementValues(foundEls, formData[key], includeHidden);
|
||||
} else {
|
||||
for (let x=0; x<foundEls.length; x++) {
|
||||
setFormElementValue(foundEls[x], request[key], includeHidden);
|
||||
setFormElementValue(foundEls[x], formData[key], includeHidden);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user