form-copypasta-crx/background.js
2024-02-01 00:53:14 +00:00

44 lines
1.1 KiB
JavaScript

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.onClicked.addListener(function(msg, tab) {
switch (msg.menuItemId) {
case "copy_form":
copyFormContextMenu(tab);
break;
case "paste_form":
pastFormContextMenu(tab);
break;
default: break;
}
});
function copyFormContextMenu(tab) {
chrome.tabs.sendMessage(tab.id, { action: "copy" }, function(response) {
if (response) {
chrome.storage.local.set({ 'clipboard': JSON.stringify(response) },function() {
chrome.contextMenus.update("paste_form", { enabled: true });
});
} else {
// Reload required
}
});
}
function pastFormContextMenu(tab) {
chrome.storage.local.get('clipboard', function(response) {
try {
let msgData = JSON.parse(response.clipboard);
msgData.action = "paste";
chrome.tabs.sendMessage(tab.id, msgData);
} catch (err) {
// Do nothing
}
});
}