diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec8069b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.buildpath +.project +.gitignore diff --git a/README.md b/README.md index 5327261..fbb671e 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,7 @@ -Simple app that opens deezer.com in own window and binds itself to media keys. +This is a fork from original dev by Natrim: [https://github.com/natrim/deezer-shortcut/](https://github.com/natrim/deezer-shortcut/) -Available at [Chrome Web Store](https://chrome.google.com/webstore/detail/deezer-shortcut/paccflbfblppaoidibhflahkogodngie) for free. +--- -NOTE: this app is NOT developed by Deezer. - -NOTE2: this app may works, or not .) - -TODO's (probadly): -- right click context menu with browser controls -- better window size handling -- editable keys -- more keys (focus, hide, ...) -- notifications - -Key binding part "lent" from https://github.com/julianxhokaxhiu/chrome-deezer-mediakeys-reloaded +Upgrades: +* Adding "like/unlike" key +* Adding notification when music changes \ No newline at end of file diff --git a/assets/app.js b/assets/app.js index e919f20..32b1dba 100644 --- a/assets/app.js +++ b/assets/app.js @@ -1,8 +1,48 @@ /* global chrome, BrowserControl, TitleBar */ var viewIsLoaded = false; + window.onload = function () { + // Used to replace some html entities into normal chars + var htmlStr = function(str){ + return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); + }; + var webview = document.querySelector('#deezerview'); + // Add listener for consolemessage from webview + webview.addEventListener('consolemessage', function(e) { + try { + var result = JSON.parse(e.message); + if (result){ + if (result.id === 'deezer_shortcut_handle_events'){ + switch (result.action){ + case 'change_music': + // Get image from Deezer + var xhr = new XMLHttpRequest(); + xhr.open('GET', result.data.nextCover, true); + xhr.responseType = 'blob'; + xhr.onload = function(e) { + // Create & show notification + chrome.notifications.create('music-changed',{ + type: 'basic', + iconUrl: window.URL.createObjectURL(this.response), + title: htmlStr(result.data.nextTitle), + message: htmlStr(result.data.nextArtist) + }); + + }; + xhr.send(); + break; + } + } + } + + }catch (e){ + // Not for our purpose + } + + }); + //block ads webview.request.onBeforeRequest.addListener( function (details) { @@ -44,6 +84,30 @@ window.onload = function () { document.querySelector('#deezerview').executeScript({ code: "var script=document.createElement('script');script.textContent=\"" + source + "\";(document.head||document.documentElement).appendChild(script);script.parentNode.removeChild(script);" }); + + // Send message into host to handle music changes + webview.executeScript( + { + code: "Array.prototype.slice.call(document.querySelectorAll('div.player-track .player-track-link')).forEach(function(el){" + + "el.addEventListener('DOMSubtreeModified', function(){" + + "var nextCover, nextTitle, nextArtist = '';" + + "window.setTimeout(function(){" + + "nextCover = Array.prototype.slice.call(document.querySelectorAll('div.player-cover img'))[0].src;" + + "nextTitle = Array.prototype.slice.call(document.querySelectorAll('h2.player-track-title a.player-track-link'))[0].innerHTML;" + + "nextArtist = new Array();" + + "Array.prototype.slice.call(document.querySelectorAll('h3.player-track-artist a.player-track-link')).forEach(function(el){" + + "nextArtist.push(el.innerHTML)" + + "});" + + "nextArtist = nextArtist.join(', ');" + + "console.log(JSON.stringify({id: 'deezer_shortcut_handle_events', action: 'change_music', data: {nextTitle: nextTitle, nextArtist: nextArtist, nextCover: nextCover}}));" + + "}, 100);" + + "});" + + "});" + }, function(result){ + console.log(result); + }); + + }); webview.addEventListener('permissionrequest', function (e) { if (e.permission === 'download') { @@ -130,9 +194,11 @@ window.onload = function () { }); }; + chrome.commands.onCommand.addListener(function (command) { if (!viewIsLoaded) return; var webview = document.querySelector('#deezerview'); + switch (command) { case 'NEXT-MK': //NEXT_MK @@ -158,5 +224,11 @@ chrome.commands.onCommand.addListener(function (command) { code: "Array.prototype.slice.call(document.querySelectorAll('#player_control_pause,button.control-pause')).forEach(function(el) { el.click(); });" }); break; + case 'LIKE-MK': + //LIKE_MK + webview.executeScript({ + code: "var btns = Array.prototype.slice.call(document.querySelectorAll('a[role=\"button\"].evt-over.evt-out.action:not(.action-hide)'));if (btns.length>1){btns[1].click()}else{btns[0].click()}" + }); + break; } }); diff --git a/manifest.json b/manifest.json index ba38dac..7880aa8 100644 --- a/manifest.json +++ b/manifest.json @@ -55,6 +55,13 @@ "suggested_key": { "default": "MediaStop" } + }, + "LIKE-MK": { + "description": "Like Key", + "global": true, + "suggested_key": { + "default": "Ctrl+Shift+Up" + } } },