Skip to content

Commit

Permalink
Novo recurso de clicar na notificação e abrir a aba correspondente; P…
Browse files Browse the repository at this point in the history
…adronizando código pelo standard
  • Loading branch information
dotenorio committed Feb 5, 2016
1 parent 22faadc commit b034d90
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 75 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog #

0.2.6
- Ao clicar na notificação, a aba que emitiu é aberta

0.1.6
- Adicionando suporte para o Google Play Music
- Mudança no padrão de versionamento, 0.0.0: [mudança significativa].[novo recurso].[correções]
Expand Down
125 changes: 73 additions & 52 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global chrome */

function getTabs () {
chrome.tabs.query({url: [
'*://*.deezer.com/*',
Expand All @@ -7,104 +9,123 @@ function getTabs () {
]}, function (tabs) {
if (tabs.length > 0) {
tabs.forEach(function (tab) {
var audible = tab.audible ? 'tocando' : 'pronta';
console.log('Uma aba ' + audible + ': ' + tab.title + ' (' + tab.url + ').');
chrome.tabs.executeScript(tab.id, {file: 'observer.js'});
var audible = tab.audible ? 'tocando' : 'pronta'
console.log('Uma aba ' + audible + ': ' + tab.title + ' (' + tab.url + ').')
chrome.tabs.executeScript(tab.id, {file: 'observer.js'})
chrome.pageAction.show(tab.id)
})
} else {
console.log('Nenhuma aba tocando.')
}
});
})
}

getTabs();
getTabs()

chrome.tabs.onUpdated.addListener(function () {
getTabs();
});
getTabs()
})

var manifest = chrome.runtime.getManifest();
var manifest = chrome.runtime.getManifest()

function onNotify (title, url) {
var contextMessage = '';
var regexYoutube = new RegExp("youtube.com");
var regexDeezer = new RegExp("deezer.com");
var regexSpotify = new RegExp("spotify.com");
var regexGooglePlayMusic = new RegExp("play.google.com");
function onNotify (title, url, id) {
var contextMessage = ''
var regexYoutube = new RegExp('youtube.com')
var regexDeezer = new RegExp('deezer.com')
var regexSpotify = new RegExp('spotify.com')
var regexGooglePlayMusic = new RegExp('play.google.com')
if (regexYoutube.test(url)) {
var regexYoutubeWatch = new RegExp("youtube.com.*watch");
var regexYoutubeWatch = new RegExp('youtube.com.*watch')
if (!regexYoutubeWatch.test(url)) {
console.log('Não é uma página de vídeo do Youtube.');
return;
console.log('Não é uma página de vídeo do Youtube.')
return
}
contextMessage = 'YouTube';
contextMessage = 'YouTube'
} else if (regexDeezer.test(url)) {
contextMessage = 'Deezer';
contextMessage = 'Deezer'
} else if (regexSpotify.test(url)) {
contextMessage = 'Spotify';
contextMessage = 'Spotify'
} else if (regexGooglePlayMusic.test(url)) {
var splitGooglePlayMusic = title.split(' - ')
if (splitGooglePlayMusic.length < 2) {
console.log('Google Play Música não está tocando.');
return;
console.log('Google Play Música não está tocando.')
return
}
contextMessage = 'Google Play Music';
contextMessage = 'Google Play Music'
}
chrome.notifications.create(title, {
chrome.notifications.create(title + '_-_' + id, {
title: manifest.name,
message: title,
contextMessage: contextMessage,
type: 'basic',
iconUrl: 'icon_notification.png'
iconUrl: 'icon_notification.png',
isClickable: true
}, function () {
console.log('Notificação disparada..');
});
console.log('Notificação disparada..')
})
}

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log();
var send = request.send;
var log;
function (request, sender, sendResponse) {
console.log()
var send = request.send
var log
if (!send) {
log = 'Não existe uma mensagem válida.';
log = 'Não existe uma mensagem válida.'
} else if (!sender.tab) {
log = 'Não foi uma aba que enviou essa mensagem.';
log = 'Não foi uma aba que enviou essa mensagem.'
} else if (noNotify.indexOf(sender.tab.id) !== -1) {
log = 'As notificações desta aba estão desabilitadas.';
log = 'As notificações desta aba estão desabilitadas.'
}
if (log) {
console.log(log);
return sendResponse({message: log});
console.log(log)
return sendResponse({message: log})
}
var url = sender.tab.url;
var title = request.title || sender.tab.title;
console.log('Recebido da url: ' + url + '.');
var url = sender.tab.url
var title = request.title || sender.tab.title
var id = sender.tab.id
console.log('Recebido da url: ' + url + '.')
if (request.send === 'notify') {
onNotify(title, url);
return sendResponse({message: manifest.name + ': ' + title.trim() + '.'});
onNotify(title, url, id)
return sendResponse({message: manifest.name + ': ' + title.trim() + '.'})
}
}
);
)

var noNotify = [];
var noNotify = []

chrome.pageAction.onClicked.addListener(function (tab) {
var indexOf = noNotify.indexOf(tab.id);
var message = ' notificações para a aba ' + tab.title + '.';
var icon;
var indexOf = noNotify.indexOf(tab.id)
var message = ' notificações para a aba ' + tab.title + '.'
var icon
if (indexOf === -1) {
noNotify.push(tab.id);
icon = 'icon_pageaction_inactive.png';
console.log('Desabilitando' + message);
noNotify.push(tab.id)
icon = 'icon_pageaction_inactive.png'
console.log('Desabilitando' + message)
} else {
noNotify.splice(indexOf, 1)
icon = 'icon_pageaction_active.png';
console.log('Habilitando' + message);
icon = 'icon_pageaction_active.png'
console.log('Habilitando' + message)
}
chrome.pageAction.setIcon({
tabId: tab.id,
path: icon
})
})
})

chrome.notifications.onClicked.addListener(function (notificationId) {
console.log('Clique da notificação recebido.')
var tabId = notificationId.split('_-_')
console.log(tabId);
tabId = parseInt(tabId[1], 10)
chrome.tabs.update(tabId, {active: true}, function (tab) {
console.log('Ativando aba..')
var windowId = tab.windowId
chrome.windows.update(windowId, {focused: true}, function () {
console.log('Focando janela..')
chrome.notifications.clear(notificationId, function () {
console.log('Notificação fechada.')
})
})
})
})
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Tocando agora (beta)",
"version": "0.1.6",
"version": "0.2.6",
"description": "Extensão para notificar qual música/vídeo está tocando no Deezer, Youtube, Spotify ou Google Play Music.",
"background": {
"scripts": [
Expand Down
46 changes: 24 additions & 22 deletions observer.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
/* global chrome */

function emmitNotify (title) {
if (!chrome || !chrome.runtime) return;
if (!chrome || !chrome.runtime) return
chrome.runtime.sendMessage(chrome.runtime.id, {
send: 'notify',
send: 'notify',
title: title
}, function(response) {
console.log(response.message);
});
}, function (response) {
console.log(response.message)
})
}

var lastTitle;
var target = document.querySelector('head > title');
var lastTitle
var target = document.querySelector('head > title')

var observer = new window.MutationObserver(
function (mutations) {
var title;
var title
mutations.forEach(
function (mutation) {
title = mutation.target.textContent.trim();
title = mutation.target.textContent.trim()
var titleReturn = [
'YouTube',
'Spotify',
'Spotify Web Player',
'Spotify Web Player - Spotify',
'Google Play Music',
'Google Play Música',
'Google Play Música'
]
if (titleReturn.indexOf(title) !== -1) return;
if (title[0] === '\u25B6') title = title.substr(2, title.length);
title = title.replace(/- (YouTube|Spotify|Google Play Music|Google Play Música)/i, '');
if (title === lastTitle) return;
emmitNotify(title);
lastTitle = title;
if (titleReturn.indexOf(title) !== -1) return
if (title[0] === '\u25B6') title = title.substr(2, title.length)
title = title.replace(/- (YouTube|Spotify|Google Play Music|Google Play Música)/i, '')
if (title === lastTitle) return
emmitNotify(title)
lastTitle = title
}
);
)
}
);
)

observer.observe(target, {
subtree: true,
observer.observe(target, {
subtree: true,
characterData: true,
childList: true
});
childList: true
})

0 comments on commit b034d90

Please sign in to comment.