Microsoft Teams Notifications

Creates browser notifications for the Web-based Teams application. Useful in Linux (in Linux notifications do not work). Tested in Chrome 69.

  1. // ==UserScript==
  2. // @name Microsoft Teams Notifications
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Creates browser notifications for the Web-based Teams application. Useful in Linux (in Linux notifications do not work). Tested in Chrome 69.
  6. // @author David López Castellote
  7. // @match https://teams.microsoft.com/*
  8. // @grant none
  9. // ==/UserScript==
  10. (function() {
  11. 'use strict';
  12.  
  13. function notifyMe(numberOfMessages) {
  14. // Let's check if the browser supports notifications
  15. if (!("Notification" in window)) {
  16. alert("This browser does not support desktop notifications.");
  17. }
  18.  
  19. // Let's check whether notification permissions have already been granted
  20. else if (Notification.permission === "granted") {
  21. // If it's okay let's create a notification
  22. createNotification(numberOfMessages);
  23. }
  24.  
  25. // Otherwise, we need to ask the user for permission
  26. else if (Notification.permission !== "denied") {
  27. Notification.requestPermission(function(permission) {
  28. // If the user accepts, let's create a notification
  29. if (permission === "granted") {
  30. createNotification(numberOfMessages);
  31. }
  32. });
  33. }
  34.  
  35. }
  36.  
  37. function createNotification(numberOfMessages) {
  38. var title = "Microsoft Teams";
  39. var options = {
  40. body: "You have " + numberOfMessages + " new notifications.",
  41. icon: document.querySelector('link[rel="icon"]').href,
  42. requireInteraction: true
  43. };
  44. var notification = new Notification(title, options);
  45. notification.onclick = function() {
  46. window.focus();
  47. };
  48. }
  49.  
  50.  
  51. function setTitleObserver() {
  52. console.log('Activating Teams notifications...');
  53. requestNotificationsPermission();
  54. var target = document.querySelector('head > title');
  55. var observer = new window.WebKitMutationObserver(function(mutations) {
  56. mutations.forEach(function(mutation) {
  57. var newTitle = mutation.target.textContent;
  58. var res;
  59. try {
  60. res = newTitle.match(/(?<=\().+?(?=\))/);
  61. }
  62. catch( e ) {
  63. res = newTitle.match(/\(([^)]+)\)/);
  64. }
  65. if (res && res[0] && res[0] && document.hidden) {
  66. notifyMe(res[0]);
  67. return false;
  68. }
  69. });
  70. });
  71. observer.observe(target, {
  72. subtree: true,
  73. characterData: true,
  74. childList: true
  75. });
  76.  
  77. }
  78.  
  79. function requestNotificationsPermission() {
  80. Notification.requestPermission().then(function(result) {
  81. console.log('Permission for Teams notifications: ' + result);
  82. });
  83. }
  84.  
  85. function exitFromCurrentChat() {
  86. setInterval( function() {
  87. if( document.hidden ) {
  88. document.querySelector('ul[data-tid="active-chat-list"] li:nth-last-child(2) a').click();
  89. }
  90. }, 30000 );
  91. }
  92.  
  93. setTitleObserver();
  94. exitFromCurrentChat();
  95. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址