Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
arnissolle authored Jun 9, 2017
2 parents aa5b684 + 2135838 commit 38f39d8
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ The shortcut is `ctrl + return(enter)` (currently non-customizable). It will tri

![action button in list view](http://cl.ly/2d0N0N1U2h3P/Image%202016-02-18%20at%206.44.49%20PM.png)

#### Mute Thread
If you no longer want to receive notifications for a certain thread on GitHub, click the `Mute thread` button or using a shortcut `shift + h`, it will open a background window to load the mute thread request, and close itself when done.

![Mute thread button](https://s22.postimg.org/4cwbsva0h/687474703a2f2f636c2e6c792f32313272334d336b316330.gif)

## Installation

#### The Easy Way
Expand Down
2 changes: 1 addition & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_appName__",
"short_name": "__MSG_appShortName__",
"version": "0.7.0",
"version": "0.8.0",
"manifest_version": 2,
"description": "__MSG_appDescription__",
"homepage_url": "http://github.com/muan/github-gmail",
Expand Down
53 changes: 35 additions & 18 deletions chrome/src/bg/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,42 @@ request.onload = function() {
if(localStorage[key]) { data[key] = localStorage[key] }
}

chrome.extension.onMessage.addListener(
function(request, sender, sendMessage) {
if(request.url) {
chrome.tabs.query(
{windowId: sender.tab.windowId},
function(tabs) {
var position = sender.tab.index;
for(var i = position; i < tabs.length; i++) {
if(tabs[i].openerTabId == sender.tab.id) {
position = i
}
chrome.extension.onMessage.addListener(function(req, sender, sendMessage) {
if(req.url) {
chrome.tabs.query(
{windowId: sender.tab.windowId},
function(tabs) {
var position = sender.tab.index;
for(var i = position; i < tabs.length; i++) {
if(tabs[i].openerTabId == sender.tab.id) {
position = i
}
request.openerTabId = sender.tab.id
request.index = position + 1
chrome.tabs.create(request)
}
var mute = req.mute
delete req.mute

req.openerTabId = sender.tab.id
req.index = position + 1
chrome.tabs.create(req, function(tab) {
if (mute) listenAndCloseTab(tab, req.url, sender.tab.id)
})
} else {
sendMessage(data)
}
}
)
} else {
sendMessage(data)
}
)
})
}

function listenAndCloseTab (tab, url, originalTabId) {
var listener = setInterval(function () {
chrome.tabs.get(tab.id, function (tab) {
if (tab.status === 'complete') {
chrome.tabs.remove(tab.id)
clearInterval(listener)
// Unsubscription finished
chrome.tabs.sendMessage(originalTabId, {muteURL: url})
}
})
}, 500)
}
6 changes: 4 additions & 2 deletions chrome/src/inject/inject.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.github-link {
.github-link,
.github-mute {
text-decoration: none;
cursor: pointer;
}
.github-link:hover {
.github-link:hover,
.github-mute:hover {
border-color: rgba(0,0,0,0.2) !important;
}
35 changes: 21 additions & 14 deletions chrome/src/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ chrome.extension.sendMessage({}, function (settings) {
initForInbox()
})

chrome.runtime.onMessage.addListener(function (req) {
var element = req['muteURL'] ? document.querySelector('[href="' + req['muteURL'] + '"]') : null

if (element) {
element.innerText = "Muted!"
}
})

function initForInbox() {
window.idled = true
}
Expand All @@ -16,7 +24,7 @@ function initOnHashChangeAction(domains) {
if(domains) allDomains += domains

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

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

// Avoid multple buttons
Array.prototype.forEach.call(document.querySelectorAll('.github-link, .github-mute-button'), function (ele) {
Array.prototype.forEach.call(document.querySelectorAll('.github-link, .github-mute'), function (ele) {
ele.remove()
})

Expand All @@ -50,21 +58,20 @@ function initOnHashChangeAction(domains) {
if (url.match('notifications/unsubscribe')) {
var muteURL = url
url = github_links[github_links.length-2].href
muteLink = document.createElement('button')
muteLink.type = 'button'
muteLink.className = 'github-mute-button T-I J-J5-Ji lS T-I-ax7 ar7'
muteLink = document.createElement('a')
muteLink.className = 'github-mute T-I J-J5-Ji lS T-I-ax7 ar7'
muteLink.innerText = 'Mute thread'
muteLink.addEventListener('click', function () {
muteLink.href = muteURL

muteLink.addEventListener('click', function (evt) {
evt.preventDefault()
chrome.extension.sendMessage({url: muteURL, active: false, mute: true})
muteLink.innerHTML = '&ctdot;'
fetch(muteURL, {mode: 'no-cors'}).then(function () {
muteLink.innerText = 'Muted!'
muteLink.disabled = 'disabled'
})
})
}

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

// Mute Shortcut: bind user's combination, if a button exist and event not in a textarea
if (processRightCombinationBasedOnShortcut(muteShortcut, event) && window.idled && getVisible(document.getElementsByClassName('github-mute-button')) && notAnInput(event.target)) {
getVisible(document.getElementsByClassName('github-mute-button')).click()
if (processRightCombinationBasedOnShortcut(muteShortcut, event) && window.idled && getVisible(document.getElementsByClassName('github-mute')) && notAnInput(event.target)) {
getVisible(document.getElementsByClassName('github-mute')).click()
}
})
}
Expand Down

0 comments on commit 38f39d8

Please sign in to comment.