-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
54 lines (44 loc) · 1.2 KB
/
index.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
/* global browser, fetch, URL */
'use strict'
if (typeof browser !== 'undefined') {
var chrome = browser
}
const PR_RE = /^\/nodejs\/([^/]+)\/pull\/([^/]+)\/?$/
function updateButton (tabId, url) {
if (url === undefined) return
if (PR_RE.test(new URL(url).pathname)) {
chrome.pageAction.show(tabId)
} else {
chrome.pageAction.hide(tabId)
}
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
updateButton(tabId, changeInfo && changeInfo.url)
})
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tabInfo) => {
updateButton(activeInfo.tabId, tabInfo && tabInfo.url)
})
})
chrome.pageAction.onClicked.addListener((tab) => {
chrome.tabs.executeScript(tab.id, {
file: 'review.js'
})
})
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
fetch(request.url)
.then((res) => res.text())
.then((body) => {
sendResponse({body: body})
})
.catch(function (err) {
sendResponse({error: err})
})
return true
})
// Ensure that the active tabs in each window are set up.
chrome.tabs.query({active: true}, (tabs) => {
tabs.forEach((tab) => {
updateButton(tab.id, tab.url)
})
})