-
Notifications
You must be signed in to change notification settings - Fork 15
/
shRedirect.js
52 lines (45 loc) · 1.33 KB
/
shRedirect.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
/*
* GLOBAL VAR
*/
// Prepare default value in case no new has been saved
const STORAGE_KEY = "ASH-baseUrl";
const DEFAULT_URL = "https://whereisscihub-15390rb7o.now.sh/go/";
let config = {};
config[STORAGE_KEY] = DEFAULT_URL;
// Firefox always has both chrome and browser objects, Chrome has only chrome
var browser = browser || chrome;
let storage = browser.storage.local;
function openSciHubTab(tab, url) {
if (tab.active) {
storage.get(config, function (config) {
browser.tabs.create({
url: config[STORAGE_KEY] + url,
index: tab.index + 1
});
});
}
}
/*
* EVENT LISTENER
*/
// browserAction listener
browser.browserAction.onClicked.addListener(function (tab) { openSciHubTab(tab, tab.url) });
/**
* Add context menu to save page.
* Docs: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/ContextMenus/create
*/
browser.contextMenus.create({
id: 'openscihubtab-page',
contexts: ['page'],
title: 'Open page in Sci-hub',
onclick: function (info, tab) { openSciHubTab(tab, info.pageUrl) }
});
/**
* Add context menu to save a selected link.
*/
browser.contextMenus.create({
id: 'openscihubtab-link',
contexts: ['link'],
title: 'Open link in Sci-hub',
onclick: function (info, tab) { openSciHubTab(tab, info.linkUrl) }
});