forked from gebrkn/copytables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
94 lines (79 loc) · 5.13 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var ctx = ["page", "selection", "link", "editable"];
var menu = {};
menu.root = chrome.contextMenus.create({"title": "Table...", "enabled": false, "contexts": ctx});
menu.selectRow = chrome.contextMenus.create({ "title": "Select Row", "parentId": menu.root, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("selectRow") }});
menu.selectColumn = chrome.contextMenus.create({ "title": "Select Column", "parentId": menu.root, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("selectColumn") }});
menu.selectTable = chrome.contextMenus.create({ "title": "Select Table", "parentId": menu.root, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("selectTable") }});
menu.break1 = chrome.contextMenus.create({ "type": "separator", "parentId": menu.root, "enabled": false, "contexts": ctx });
menu.findPrevTable = chrome.contextMenus.create({ "title": "Previous Table","parentId": menu.root, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("findPrevTable") }});
menu.findNextTable = chrome.contextMenus.create({ "title": "Next Table", "parentId": menu.root, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("findNextTable") }});
menu.break2 = chrome.contextMenus.create({ "type": "separator", "parentId": menu.root, "enabled": false, "contexts": ctx });
menu.copyRich = chrome.contextMenus.create({ "title": "Copy", "parentId": menu.root, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyRich") }});
menu.copy = chrome.contextMenus.create({ "title": "Copy as...", "parentId": menu.root, "enabled": false, "contexts": ctx });
menu.copyHTML = chrome.contextMenus.create({ "title": "HTML", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyHTML") }});
menu.copyStyled = chrome.contextMenus.create({ "title": "Styled HTML", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyStyled") }});
menu.copyCSV = chrome.contextMenus.create({ "title": "CSV", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyCSV") }});
menu.copyText = chrome.contextMenus.create({ "title": "Text-Only", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyText") }});
// Send a command to tabs.
function sendCommand(cmd, broadcast, fn) {
var qry = broadcast ? {} : {active: true, currentWindow: true};
chrome.tabs.query(qry, function(tabs) {
tabs.forEach(function(tab) {
chrome.tabs.sendMessage(tab.id, {command: cmd}, fn || function(r) {});
});
});
}
// Menu selection - dispatch the message to the content.js
function menuClick(cmd) {
sendCommand(cmd);
}
// Notify tab that it's activated.
chrome.tabs.onActivated.addListener(function() {
sendCommand("activate");
});
// Content command - handle a message from the content.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
switch(message.command) {
case "copyText":
case "copyHTML":
case "copyStyled":
case "copyCSV":
var t = document.getElementById("___copytables_clipboard___");
t.value = message.content;
t.focus();
t.select();
document.execCommand("copy");
t.value = "";
sendResponse({});
break;
case "copyRich":
var t = document.getElementById("___copytables_div___");
t.contentEditable = true;
t.innerHTML = message.content;
var range = document.createRange();
range.selectNodeContents(t);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
t.innerHTML = "";
sendResponse({});
break;
case "menuUpdate":
var s = message.state;
chrome.contextMenus.update(menu.root, {enabled:s.hasTables});
chrome.contextMenus.update(menu.selectRow, {enabled:s.canSelect});
chrome.contextMenus.update(menu.selectColumn, {enabled:s.canSelect});
chrome.contextMenus.update(menu.selectTable, {enabled:s.canSelect});
chrome.contextMenus.update(menu.findPrevTable, {enabled:s.hasTables});
chrome.contextMenus.update(menu.findNextTable, {enabled:s.hasTables});
chrome.contextMenus.update(menu.copyRich, {enabled:s.canCopy});
chrome.contextMenus.update(menu.copy, {enabled:s.canCopy});
chrome.contextMenus.update(menu.copyHTML, {enabled:s.canCopy});
chrome.contextMenus.update(menu.copyStyled, {enabled:s.canCopy});
chrome.contextMenus.update(menu.copyCSV, {enabled:s.canCopy});
chrome.contextMenus.update(menu.copyText, {enabled:s.canCopy});
sendResponse({});
break;
}
});