This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
183 lines (168 loc) · 5.88 KB
/
popup.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This extension demonstrates using chrome.downloads.download() to
// download URLs.
var allLinks = [];
var visibleLinks = [];
// Display all visible links.
function showLinks() {
var linksTable = document.getElementById('links');
while (linksTable.children.length > 1) {
linksTable.removeChild(linksTable.children[linksTable.children.length - 1])
}
for (var i = 0; i < visibleLinks.length; ++i) {
var row = document.createElement('tr');
var col0 = document.createElement('td');
var col1 = document.createElement('td');
var checkbox = document.createElement('input');
checkbox.checked = true;
checkbox.type = 'checkbox';
checkbox.id = 'check' + i;
col0.appendChild(checkbox);
col1.innerText = visibleLinks[i];
col1.style.whiteSpace = 'nowrap';
col1.onclick = function() {
checkbox.checked = !checkbox.checked;
}
row.appendChild(col0);
row.appendChild(col1);
linksTable.appendChild(row);
}
}
// Toggle the checked state of all visible links.
function toggleAll() {
var checked = document.getElementById('toggle_all').checked;
for (var i = 0; i < visibleLinks.length; ++i) {
document.getElementById('check' + i).checked = checked;
}
}
// Re-filter allLinks into visibleLinks and reshow visibleLinks.
function filterLinks() {
var filterValue = document.getElementById('filter').value;
if (document.getElementById('regex').checked) {
visibleLinks = allLinks.filter(function(link) {
return link.match(filterValue);
});
} else {
var terms = filterValue.split(' ');
visibleLinks = allLinks.filter(function(link) {
for (var termI = 0; termI < terms.length; ++termI) {
var term = terms[termI];
if (term.length != 0) {
var expected = (term[0] != '-');
if (!expected) {
term = term.substr(1);
if (term.length == 0) {
continue;
}
}
var found = (-1 !== link.indexOf(term));
if (found != expected) {
return false;
}
}
}
return true;
});
}
showLinks();
}
// Add links to allLinks and visibleLinks, sort and show them. send_links.js is
// injected into all frames of the active tab, so this listener may be called
// multiple times.
chrome.extension.onRequest.addListener(function(links) {
//console.log("links: "+links.length);
if (links.length > 0) {
for (var index in links) {
allLinks.push(links[index]);
}
allLinks.sort();
visibleLinks = allLinks;
showLinks();
}
else {
$("#content_section").empty();
$("#content_section").append("<div type=\"button\" class=\"btn btn-warning\" style=\"width:100%\">No links found on this page</div>");
}
});
function getSelectedLinks(visibleLinks) {
var checkedLinks = [];
for (var i=0; i<visibleLinks.length; i++) {
if (document.getElementById('check' + i).checked) {
checkedLinks.push(visibleLinks[i]);
}
}
return checkedLinks;
}
function sendToGalaxy() {
var galaxy_url = document.getElementById('galaxy').value;
console.log("galaxy_url: "+galaxy_url);
/*var galaxy_user = document.getElementById('user').value;
console.log("galaxy_user: "+galaxy_user);
var galaxy_pass = document.getElementById('password').value;
console.log("galaxy_pass: "+galaxy_pass);*/
var api_key = document.getElementById('api_key').value;
console.log("api key: "+api_key);
var collection_name = undefined;
if ((document.getElementById('collection_name').value).trim() !== "")
collection_name = document.getElementById('collection_name').value;
console.log("collection_name: "+collection_name);
var checkedLinks = getSelectedLinks(visibleLinks)
if (checkedLinks.length > 0) {
chrome.runtime.getBackgroundPage(
function (backgroundPage) {
backgroundPage.sendToGalaxy(galaxy_url, api_key, checkedLinks, collection_name);
}
);
window.close();
}
else {
console.log("Select at least one link.");
$("#nodata").show();
}
}
function collectionHandler() {
var collection_bool = $("input[name=collection]:checked").val();
console.log(collection_bool);
if (collection_bool == "true") {
console.log("collection activated");
$("#collection_name").prop("disabled", false);
//$("#collection_status").text("[enabled]");
}
else if (collection_bool == "false") {
console.log("collection disabled");
$("#collection_name").prop("disabled", true);
//$("#collection_status").text("[disabled]");
}
}
function autofill(galaxy_url, api_key) {
if (galaxy_url === undefined) galaxy_url = "";
if (api_key === undefined) api_key = "";
console.log(galaxy_url);
console.log(api_key);
$('#galaxy').val(galaxy_url);
$('#api_key').val(api_key);
}
// Set up event handlers and inject send_links.js into all frames in the active tab.
window.onload = function() {
document.getElementById('filter').onkeyup = filterLinks;
document.getElementById('regex').onchange = filterLinks;
document.getElementById('toggle_all').onchange = toggleAll;
document.getElementById('send0').onclick = sendToGalaxy;
document.getElementById('send1').onclick = sendToGalaxy;
//document.getElementsByName('collection').onclick = collectionHandler;
document.getElementById('coll_false').onclick = collectionHandler;
document.getElementById('coll_true').onclick = collectionHandler;
chrome.windows.getCurrent(function (currentWindow) {
chrome.tabs.query({active: true, windowId: currentWindow.id}, function(activeTabs) {
chrome.tabs.executeScript(activeTabs[0].id, {file: 'send_links.js', allFrames: true});
});
});
// autofill galaxu_url and api_key
chrome.runtime.getBackgroundPage(
function (backgroundPage) {
backgroundPage.autofill();
}
);
};