-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
64 lines (60 loc) · 2.27 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
/** ******************* Configures the PageAction activation ******************* */
// Based on Sample code from Chromium Extensions docs:
// https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/pageAction/pageaction_by_url/background.js
// Docs: https://developer.chrome.com/extensions/declarativeContent
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostContains: '.toptal.com' } //FIXME: also fires on platform pages
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
/** ******************* Configures the Context Menu entry ******************* */
chrome.contextMenus.removeAll()
chrome.contextMenus.create({
id: 'copy',
title: 'Copy URL with my referral hashtag',
contexts: ['page','link'],
documentUrlPatterns: ['*://*.toptal.com/*'],
targetUrlPatterns: ['*://*.toptal.com/*']
})
const $body = document.querySelector('body')
chrome.contextMenus.onClicked.addListener(info => {
const url = info.linkUrl || info.pageUrl
const hashtag = localStorage.hashtag
if (hashtag) {
const el = document.createElement('input')
el.value = `${url}#${hashtag}`
$body.appendChild(el)
el.select()
document.execCommand('copy')
} else {
chrome.notifications.create('no-hashtag', {
type: 'basic',
iconUrl: 'icons/logo-trans-256.png',
title: 'Whoops... No hashtag set',
message: "It seems you have yet to configure your referral URL. Find it in the Platform and paste in the extension popup.\nThe extension button could be hidden in your Chrome Menu.",
requireInteraction: true,
buttons: [ { title: 'Go to Platform' } ]
})
}
})
chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex) => {
switch (notificationId) {
case 'no-hashtag':
switch (buttonIndex) {
case 0:
chrome.tabs.create({ url: 'https://www.toptal.com/platform/referrals/instructions' })
break
}
break
}
chrome.notifications.clear(notificationId)
})