-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
224 lines (179 loc) · 5.91 KB
/
index.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { Plugin, PluginBuilder } from 'plugin-lib';
import * as styles from './style.css';
let HIGHLIGHTED_LINK_MARGIN = 100;
class GooglePage {
links: HTMLElement[];
nextPage: HTMLAnchorElement;
prevPage: HTMLAnchorElement;
tabsDict: object;
constructor() {
this.links = Array.prototype.slice.call(document.querySelectorAll('h3.r a'));
this.tabsDict = {
images: document.querySelector('a[class="q qs"][href*="&tbm=isch"]'),
videos: document.querySelector('a[class="q qs"][href*="&tbm=vid"]'),
maps: document.querySelector('a[class="q qs"][href*="maps.google."]'),
news: document.querySelector('a[class="q qs"][href*="&tbm=nws"]'),
shopping: document.querySelector('a[class="q qs"][href*="&tbm=shop"]')
};
this.nextPage = document.querySelector('#pnnext');
this.prevPage = document.querySelector('#pnprev');
if (this.links.length > 0) {
updateFocusedLink(this.links[0]);
}
}
getLink(index: number) {
return this.links[index];
}
getLinkCount() : number {
return this.links.length;
}
getNextPage() : HTMLAnchorElement {
return this.nextPage;
}
getPreviousPage() : HTMLAnchorElement {
return this.prevPage;
}
}
//////////////////////
// Helper functions //
//////////////////////
function getSearchInput() {
return document.getElementById('lst-ib');
}
function getLink(page, index) {
return page.getLink(index);
}
function updateFocusedLinkAtIndex(index) {
let link = getLink(page, index);
updateFocusedLink(link);
}
function updateFocusedLink(link) {
link.className += ' ' + styles.test;
let linkPos = link.getBoundingClientRect().top;
if (linkPos < HIGHLIGHTED_LINK_MARGIN) {
// If the link is at the top of the screen
window.scrollTo(window.pageXOffset, window.pageYOffset + linkPos - HIGHLIGHTED_LINK_MARGIN);
} else if (linkPos > window.innerHeight - HIGHLIGHTED_LINK_MARGIN) {
// If the link is below the screen
window.scrollTo(window.pageXOffset, window.pageYOffset + (linkPos - window.innerHeight + HIGHLIGHTED_LINK_MARGIN + link.clientHeight));
}
}
function clearHighlights(page) {
for (let i = 0; i < page.getLinkCount(); i++) {
let link = getLink(page, i);
link.classList.remove(styles.test);
}
}
//////////////////////
// Application code //
//////////////////////
let page = new GooglePage();
let shortcuts = {
nextLink: 'option+j',
previousLink: 'option+k',
openLink: 'enter',
openLinkNewTab: 'command+enter',
nextPage: 'option+l',
previousPage: 'option+h',
focusSearchInput: 'option+/',
highlightSearchInput: 'command+/',
navigateAllTab: 'option+a',
navigateImagesTab: 'option+i',
navigateVideosTab: 'option+v',
navigateMapsTab: 'option+m',
navigateNewsTab: 'option+n',
navigateShoppingTab: 'option+s',
};
///////////////////////
let pb = new PluginBuilder();
pb.setPluginName('google');
pb.addDomainName('google.com');
pb.addDomainName('google.ca');
pb.setInitialState({
linkIndex: 0
});
pb.registerShortcut('Next link', shortcuts.nextLink, (event, state) => {
// Clear existing classes
clearHighlights(page);
let nextIndex = Math.min(state.linkIndex + 1, page.getLinkCount() - 1);
state.set({ linkIndex: nextIndex });
// Update which link is focused
updateFocusedLinkAtIndex(state.linkIndex);
});
pb.registerShortcut('Previous link', shortcuts.previousLink, (event, state) => {
// Clear existing classes
clearHighlights(page);
let prevIndex = Math.max(state.linkIndex - 1, 0);
state.set({ linkIndex: prevIndex });
// Update which link is focused
updateFocusedLinkAtIndex(state.linkIndex);
});
function triggerMouseEvent(node, eventType) {
let clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent(eventType, true, true);
node.dispatchEvent(clickEvent);
}
function navigate(link) {
if (link) {
location.href = link.href;
}
}
pb.registerShortcut('Open link', shortcuts.openLink, (event, state) => {
navigate(getLink(page, state.linkIndex));
});
pb.registerShortcut('Open link in new tab', shortcuts.openLinkNewTab, (event, state) => {
let link = getLink(page, state.linkIndex);
window.open(link.href, '_blank');
});
pb.registerShortcut('Next page', shortcuts.nextPage, (event, state) => {
navigate(page.getNextPage());
});
pb.registerShortcut('Previous page', shortcuts.previousPage, (event, state) => {
navigate(page.getPreviousPage());
});
pb.registerShortcut('Focus on Search Input', shortcuts.focusSearchInput, (event, state) => {
let searchInput = getSearchInput();
searchInput.focus();
event.preventDefault();
event.stopPropagation();
// Always move cursor to the back
// @ts-ignore
let val = searchInput.value;
// @ts-ignore
searchInput.value = '';
// @ts-ignore
searchInput.value = val;
});
pb.registerShortcut('Highlight Search Input', shortcuts.highlightSearchInput, (event, state) => {
let searchInput = getSearchInput();
searchInput.focus();
event.preventDefault();
event.stopPropagation();
// @ts-ignore
searchInput.setSelectionRange(0, searchInput.value.length);
});
pb.registerShortcut('Navigate to all tab', shortcuts.navigateAllTab, (event, state) => {
location.href = location.href.split('&')[0];
});
pb.registerShortcut('Navigate to images tab', shortcuts.navigateImagesTab, (event, state) => {
// @ts-ignore
navigate(page.tabsDict.images);
});
pb.registerShortcut('Navigate to videos tab', shortcuts.navigateVideosTab, (event, state) => {
// @ts-ignore
navigate(page.tabsDict.videos);
});
pb.registerShortcut('Navigate to maps tab', shortcuts.navigateMapsTab, (event, state) => {
// @ts-ignore
navigate(page.tabsDict.maps);
});
pb.registerShortcut('Navigate to news tab', shortcuts.navigateNewsTab, (event, state) => {
// @ts-ignore
navigate(page.tabsDict.news);
});
pb.registerShortcut('Navigate to shopping tab', shortcuts.navigateShoppingTab, (event, state) => {
// @ts-ignore
navigate(page.tabsDict.shopping);
});
let plugin = pb.build();
export default plugin;