Skip to content

Commit 47ff893

Browse files
authored
Merge pull request muan#66 from muan/patch
Fix mute button and clean github-link URL
2 parents 4413511 + ddbd751 commit 47ff893

3 files changed

Lines changed: 60 additions & 34 deletions

File tree

chrome/src/bg/background.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,42 @@ request.onload = function() {
99
if(localStorage[key]) { data[key] = localStorage[key] }
1010
}
1111

12-
chrome.extension.onMessage.addListener(
13-
function(request, sender, sendMessage) {
14-
if(request.url) {
15-
chrome.tabs.query(
16-
{windowId: sender.tab.windowId},
17-
function(tabs) {
18-
var position = sender.tab.index;
19-
for(var i = position; i < tabs.length; i++) {
20-
if(tabs[i].openerTabId == sender.tab.id) {
21-
position = i
22-
}
12+
chrome.extension.onMessage.addListener(function(req, sender, sendMessage) {
13+
if(req.url) {
14+
chrome.tabs.query(
15+
{windowId: sender.tab.windowId},
16+
function(tabs) {
17+
var position = sender.tab.index;
18+
for(var i = position; i < tabs.length; i++) {
19+
if(tabs[i].openerTabId == sender.tab.id) {
20+
position = i
2321
}
24-
request.openerTabId = sender.tab.id
25-
request.index = position + 1
26-
chrome.tabs.create(request)
22+
}
23+
var mute = req.mute
24+
delete req.mute
25+
26+
req.openerTabId = sender.tab.id
27+
req.index = position + 1
28+
chrome.tabs.create(req, function(tab) {
29+
if (mute) listenAndCloseTab(tab, req.url, sender.tab.id)
2730
})
28-
} else {
29-
sendMessage(data)
30-
}
31+
}
32+
)
33+
} else {
34+
sendMessage(data)
3135
}
32-
)
36+
})
37+
}
38+
39+
function listenAndCloseTab (tab, url, originalTabId) {
40+
var listener = setInterval(function () {
41+
chrome.tabs.get(tab.id, function (tab) {
42+
if (tab.status === 'complete') {
43+
chrome.tabs.remove(tab.id)
44+
clearInterval(listener)
45+
// Unsubscription finished
46+
chrome.tabs.sendMessage(originalTabId, {muteURL: url})
47+
}
48+
})
49+
}, 500)
3350
}

chrome/src/inject/inject.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
.github-link {
1+
.github-link,
2+
.github-mute {
23
text-decoration: none;
34
cursor: pointer;
45
}
5-
.github-link:hover {
6+
.github-link:hover,
7+
.github-mute:hover {
68
border-color: rgba(0,0,0,0.2) !important;
79
}

chrome/src/inject/inject.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ chrome.extension.sendMessage({}, function (settings) {
77
initForInbox()
88
})
99

10+
chrome.runtime.onMessage.addListener(function (req) {
11+
var element = req['muteURL'] ? document.querySelector('[href="' + req['muteURL'] + '"]') : null
12+
13+
if (element) {
14+
element.innerText = "Muted!"
15+
}
16+
})
17+
1018
function initForInbox() {
1119
window.idled = true
1220
}
@@ -16,7 +24,7 @@ function initOnHashChangeAction(domains) {
1624
if(domains) allDomains += domains
1725

1826
// Take string -> make array -> make queries -> avoid nil -> join queries to string
19-
var selectors = allDomains.replace(/\s/, '').split(',').map(function (name) {
27+
var selectors = allDomains.replace(/\s/g, '').split(',').map(function (name) {
2028
if (name.length) return (".AO [href*='" + name + "']")
2129
}).filter(function (name) { return name }).join(", ")
2230

@@ -38,7 +46,7 @@ function initOnHashChangeAction(domains) {
3846
var github_links = reject_unwanted_paths(mail_body.querySelectorAll(selectors))
3947

4048
// Avoid multple buttons
41-
Array.prototype.forEach.call(document.querySelectorAll('.github-link, .github-mute-button'), function (ele) {
49+
Array.prototype.forEach.call(document.querySelectorAll('.github-link, .github-mute'), function (ele) {
4250
ele.remove()
4351
})
4452

@@ -50,21 +58,20 @@ function initOnHashChangeAction(domains) {
5058
if (url.match('notifications/unsubscribe')) {
5159
var muteURL = url
5260
url = github_links[github_links.length-2].href
53-
muteLink = document.createElement('button')
54-
muteLink.type = 'button'
55-
muteLink.className = 'github-mute-button T-I J-J5-Ji lS T-I-ax7 ar7'
61+
muteLink = document.createElement('a')
62+
muteLink.className = 'github-mute T-I J-J5-Ji lS T-I-ax7 ar7'
5663
muteLink.innerText = 'Mute thread'
57-
muteLink.addEventListener('click', function () {
64+
muteLink.href = muteURL
65+
66+
muteLink.addEventListener('click', function (evt) {
67+
evt.preventDefault()
68+
chrome.extension.sendMessage({url: muteURL, active: false, mute: true})
5869
muteLink.innerHTML = '&ctdot;'
59-
fetch(muteURL, {mode: 'no-cors'}).then(function () {
60-
muteLink.innerText = 'Muted!'
61-
muteLink.disabled = 'disabled'
62-
})
6370
})
6471
}
6572

66-
// Go to thread instead of .diff link (pull request notifications)
67-
url = url.match(/\.diff/) ? url.slice(0, url.length-5) : url
73+
// Go to thread instead of diffs or file views
74+
if (url.match(/^(.+\/(issue|pull)\/\d+)/)) url = url.match(/^(.+\/(issue|pull)\/\d+)/)[1]
6875
var link = document.createElement('a')
6976
link.href = url
7077
link.className = 'github-link T-I J-J5-Ji lS T-I-ax7 ar7'
@@ -116,8 +123,8 @@ function initShortcuts(shortcut, backgroundShortcut, muteShortcut) {
116123
}
117124

118125
// Mute Shortcut: bind user's combination, if a button exist and event not in a textarea
119-
if (processRightCombinationBasedOnShortcut(muteShortcut, event) && window.idled && getVisible(document.getElementsByClassName('github-mute-button')) && notAnInput(event.target)) {
120-
getVisible(document.getElementsByClassName('github-mute-button')).click()
126+
if (processRightCombinationBasedOnShortcut(muteShortcut, event) && window.idled && getVisible(document.getElementsByClassName('github-mute')) && notAnInput(event.target)) {
127+
getVisible(document.getElementsByClassName('github-mute')).click()
121128
}
122129
})
123130
}

0 commit comments

Comments
 (0)