Skip to content

Instantly share code, notes, and snippets.

@fqj1994
Last active September 25, 2015 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fqj1994/e6ebe78212747051f694 to your computer and use it in GitHub Desktop.
Save fqj1994/e6ebe78212747051f694 to your computer and use it in GitHub Desktop.
put netease music lyric to system bar
这个 gist 实现把网易云音乐的歌词显示到浏览器以外的 bar 或者 tray 上。
1. 浏览器需要单独一个窗口移到另外的 worksapce,不能是一个窗口里面的一个tab。
2. 需要手动将歌词点开
3. 配合 https://github.com/fqj1994/dotfiles 里的 xmobar(或者其他bar从一个pipe里面读了立即显示的) 和 xinitrc(主要注意要保持那个pipe不被close) 配置。
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
self.send_response(200)
self.end_headers()
fp = open("/tmp/xmobar-info", "w")
length = int(self.headers.getheader('content-length'))
fp.write(self.rfile.read(length).replace("\n", " ") + "\n")
fp.close()
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
if __name__ == '__main__':
server = ThreadedHTTPServer(('localhost', 14332), Handler)
server.serve_forever()
// ==UserScript==
// @name NetEase Music
// @namespace http://your.homepage/
// @version 0.1
// @description enter something useful
// @author You
// @require https://code.jquery.com/jquery-2.1.4.min.js
// @match http://music.163.com/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
for (var i = 0; i < mutation.addedNodes.length; i++) {
var node = mutation.addedNodes[i];
if (node.getAttribute && node.getAttribute("class") && node.getAttribute("class") == "list") {
$(".listlyric").hide();
$(".listlyric").after("<div class=\"listlyric\" id=\"testnewlyric\"></div>") // use custom non-animated lyrics to reduce CPU usage
detect_lyrics();
}
}
});
});
observer.observe(document, {childList: true, subtree: true});
function detect_lyrics() {
var prev = "";
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var name = mutation.attributeName,
newValue = mutation.target.getAttribute(name),
oldValue = mutation.oldValue;
if (name == "class" && oldValue == "j-item" && newValue == "j-item z-sel" && mutation.target.innerText != prev) {
prev = mutation.target.innerText;
var prev_lyric = '', next_lyric = '';
if ($(mutation.target).prev().html()) {
prev_lyric = "<p class=\"j-item\">" + $(mutation.target).prev().html() + "</p>";
}
var current_lyric = "<p class=\"j-item z-sel\">" + mutation.target.innerHTML + "</p>";
if ($(mutation.target).next().html()) {
next_lyric = "<p class=\"j-item\">" + $(mutation.target).next().html() + "</p>";
}
$("#testnewlyric").html(prev_lyric + current_lyric + next_lyric);
GM_xmlhttpRequest({
method: 'POST',
url: 'http://127.0.0.1:14332/',
data: mutation.target.innerText
});
}
});
});
observer.observe(document.getElementsByClassName("listlyric")[0], { attributes: true, attributeOldValue:true, subtree: true});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment