Change Facebook Notification Sound

Small script for a request https://greasyfork.org/uk/forum/discussion/6631/change-facebook-notification-sound-convert-chrome-extension-to-userscript

当前为 2015-10-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Change Facebook Notification Sound
  3. // @namespace cfs
  4. // @description Small script for a request https://greasyfork.org/uk/forum/discussion/6631/change-facebook-notification-sound-convert-chrome-extension-to-userscript
  5. // @include https://facebook.com/*
  6. // @include https://www.facebook.com/*
  7. // @version 0.1.3
  8. // @grant none
  9. // @noframes
  10. // @run-at document-end
  11. // @author Dexmaster
  12. // @date 2015-10-25
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. "use strict";
  17.  
  18. var counter = 0,
  19. soundUri = 'https://instaud.io/_/djS.mp3', // input sound link here
  20. interval = 5, // number of seconds between checks
  21. audio;
  22.  
  23. var ready = function (fn) {
  24. if (document.readyState !== 'loading') {
  25. fn();
  26. } else {
  27. document.addEventListener('DOMContentLoaded', fn);
  28. }
  29. };
  30.  
  31. var countUnread = function () {
  32. var els = document.querySelectorAll("._51jx"),
  33. sum = Array.prototype.map.call(els, function (el) {
  34. return parseInt(el.innerHTML) || 0;
  35. }).reduce(function (a, b) {
  36. return a + b;
  37. }, 0);
  38. return sum;
  39. };
  40.  
  41. var numberChange = function () {
  42. var num = countUnread();
  43. if (counter !== num) {
  44. counter = num;
  45. audio.play();
  46. }
  47. };
  48.  
  49. var init = function () {
  50. audio = document.createElement('audio');
  51. audio.setAttribute('src', soundUri);
  52. // audio.setAttribute('autoplay', 'autoplay');
  53. setInterval(numberChange, interval*1000);
  54. };
  55.  
  56. ready(init);
  57.  
  58. }());