form-copypasta-crx/background.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-02-01 00:53:14 +00:00
chrome.runtime.onInstalled.addListener(function() {
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_form2", "title": "Paste Form (with Hidden)", "contexts": ["editable"], enabled: false });
2024-02-01 00:53:14 +00:00
});
chrome.contextMenus.onClicked.addListener(function(msg, tab) {
switch (msg.menuItemId) {
case "copy_form":
copyFormContextMenu(tab);
break;
case "paste_form":
pasteFormContextMenu(tab, false);
break;
case "paste_form2":
pasteFormContextMenu(tab, true);
2024-02-01 00:53:14 +00:00
break;
default: break;
}
});
function copyFormContextMenu(tab) {
chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) {
try {
2024-02-01 00:53:14 +00:00
chrome.storage.local.set({ 'clipboard': JSON.stringify(response) },function() {
chrome.contextMenus.update("paste_form", { enabled: true });
});
} catch (err) {
// Do nothing
2024-02-01 00:53:14 +00:00
}
});
}
function pasteFormContextMenu(tab, includeHiddenEl) {
2024-02-01 00:53:14 +00:00
chrome.storage.local.get('clipboard', function(response) {
try {
const payload = {
action: (includeHiddenEl === true ? "paste2" : "paste"),
formData: JSON.parse(response.clipboard)
};
2024-02-01 00:53:14 +00:00
chrome.tabs.sendMessage(tab.id, payload);
2024-02-01 00:53:14 +00:00
} catch (err) {
// Do nothing
}
});
}