Skip to content

Instantly share code, notes, and snippets.

@bxshi
Last active December 11, 2015 18:09
Show Gist options
  • Save bxshi/4639820 to your computer and use it in GitHub Desktop.
Save bxshi/4639820 to your computer and use it in GitHub Desktop.
A webkit-notification plugin for [Web WeChat](https://web.wechatapp.com/) **I only tested chrome, and if it could run on other browser, please notice me :)**
// ==UserScript==
// @name Desktop Notification 4 Web WeChat
// @namespace https://gist.github.com/4639820
// @version 1.0
// @description nothing
// @match https://web.wechatapp.com/
// @copyright 2013+, Carmack.Shi
// ==/UserScript==
var mainFunc = function () {
var checkUnRead = function () {
var unReadSpans = $('span.unreadDot').text();
var unReadCount = 0;
for (var i = 0; i < unReadSpans.length; i++) {
unReadCount += parseInt(unReadSpans[i]);
}
return unReadCount;
};
var doCheck = function () {
if (window.webkitNotifications.checkPermission() == 0) { // already permitted
var t = checkUnRead();
if (t > 0) {
window.webkitNotifications.createNotification("", "WeChat Notification", "You got " + t + " unread message").show();
}
}
}
// check for notifications support
if (!window.webkitNotifications) {
alert("Sorry, your bowrser currently not support desktop notification.");
} else {
if (window.webkitNotifications.checkPermission() == 1) { //PERMISSION_NOT_ALLOWED
alert('Please allow notification by click the bottom link, and them click allow on the top.');
$('.footer').append('<div><a onclick="void(window.webkitNotifications.requestPermission());" >Start desktop permission</a></div>')
window.webkitNotifications.requestPermission();
}
setInterval(doCheck, 1000);
}
}
// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
script.addEventListener('load', function () {
var script = document.createElement("script");
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
addJQuery(mainFunc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment