-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiframe.js
More file actions
192 lines (160 loc) · 5.63 KB
/
Copy pathiframe.js
File metadata and controls
192 lines (160 loc) · 5.63 KB
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
184
185
186
187
188
189
190
191
192
import * as UIComponentMessenger from "./pages/ui_component_messenger.js"
/*
* NOTE: This script gets activated from an HTML file
*/
let pagePort = null;
let handleMessage = null;
let ui = null;
async function activate() {
if (ui == null) {
ui = new HarpoonUI();
}
// Fetch pinned pages once
chrome.runtime.sendMessage({ type: "GET_PINNED_PAGES" }, (response) => {
ui.pinnedPages = response.pinnedPages
const container = document.getElementById('pins');
container.innerHTML = '';
ui.pinnedPages.forEach((tabId, idx) => {
const item = document.createElement('div');
item.className = 'harpoon-item';
if (idx === 0) {
item.classList.add("active");
}
const keySpan = document.createElement('span');
keySpan.className = 'harpoon-key';
keySpan.textContent = `${(idx + 1) % 10}`;
item.appendChild(keySpan);
const label = document.createElement('span');
label.className = 'harpoon-label';
if (tabId !== -1) {
chrome.tabs.get(tabId, (tab) => {
if (chrome.runtime.lastError || !tab || !tab.url) {
label.textContent = "(closed)";
label.classList.add("empty");
} else {
label.textContent = tab.title.substring(0, 20) + (tab.title.length > 18 ? "..." : "") || "(no title)";
label.classList.remove("empty");
chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => {
if (tabs[0].id === tabId) {
keySpan.classList.add("selected");
}
});
}
});
} else {
label.textContent = "(empty)";
label.classList.add("empty");
}
item.appendChild(label);
container.appendChild(item);
});
});
}
class HarpoonUI {
box;
// idx
currTab = 0;
pinnedPages;
constructor() {
this.onKeyDown = this.onKeyDown.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.update = this.update.bind(this);
this.hide = this.hide.bind(this);
this.initDom();
}
async update() {
}
onKeyDown(event) {
// NOTE: Users could technically delete marks
// while the iframe is open from the icon, but
// I'm leaving that to undefined behaviour instead
// of fetching every time
if (event.key === "j") {
var labels = document.querySelectorAll(".harpoon-item");
const tab = labels[this.currTab];
tab.classList.remove("active");
this.currTab = (this.currTab + 1) % 10;
const newCurr = labels[this.currTab];
newCurr.classList.add("active");
} else if (event.key === "k") {
var labels = document.querySelectorAll(".harpoon-item");
const tab = labels[this.currTab];
tab.classList.remove("active");
this.currTab = this.currTab === 0 ? labels.length - 1 : this.currTab - 1;
const newCurr = labels[this.currTab];
newCurr.classList.add("active");
} else if (event.key === "x") {
var labels = document.querySelectorAll(".harpoon-label");
var keys = document.querySelectorAll(".harpoon-key");
const tab = labels[this.currTab];
const key = keys[this.currTab];
key.classList.remove("selected");
tab.classList.add("empty");
tab.innerHTML = "(empty)";
chrome.runtime.sendMessage({
type: "REMOVE_PINNED_PAGE",
index: this.currTab
});
} else if (event.key === "Enter") {
var idx = this.currTab + 1;
this.hide();
chrome.runtime.sendMessage({
type: "MOVE_TO_PINNED_PAGE",
index: idx
});
} else if (event.key === "Escape") {
this.hide();
} else {
for (let i = 0; i < 10; i++) {
if (event.key == i) {
this.hide();
chrome.runtime.sendMessage({
type: "MOVE_TO_PINNED_PAGE",
index: i
});
break;
}
}
}
}
onKeyUp(event) {
}
hide(event) {
this.currTab = 0;
UIComponentMessenger.postMessage({ name: "hide" });
}
initDom() {
this.box = document.getElementsByClassName("harpoon-ui");
document.addEventListener("keydown", this.onKeyDown);
document.addEventListener("keyup", this.onKeyUp);
}
};
// window.addEventListener("message", registerMessage);
// async function registerMessage(event) {
// switch (event.data) {
// case "SET_IFRAME_PORT":
// break;
// default:
// console.error("HERE: Unrecognized message type");
// break;
// }
// }
function init() {
UIComponentMessenger.init();
UIComponentMessenger.registerHandler((event) => {
switch (event.data.name) {
case "hide":
ui?.hide();
break;
case "hidden":
ui?.onHidden();
break;
case "activate":
activate();
break;
default:
console.log("Unrecognized message: ", event.data.name);
}
});
}
init();