Battery Notifier for chrome on google/bing

Notify when battery reaches low

  1. // ==UserScript==
  2. // @name Battery Notifier for chrome on google/bing
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Notify when battery reaches low
  6. // @author Grok
  7. // @match https://www.bing.com/*
  8. // @match https://www.google.com/*
  9. // @match *://*.microsoft.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Check if Battery API is supported
  17. if ('getBattery' in navigator) {
  18. navigator.getBattery().then(function(battery) {
  19. // Initial check
  20. checkBatteryLevel(battery.level);
  21.  
  22. // Listen for battery level changes
  23. battery.addEventListener('levelchange', function() {
  24. checkBatteryLevel(battery.level);
  25. });
  26. });
  27. } else {
  28. console.log('Battery Status API not supported');
  29. return;
  30. }
  31.  
  32. function checkBatteryLevel(level) {
  33. // Convert level to percentage
  34. const percentage = Math.round(level * 100);
  35.  
  36. // Check if battery is at or below percent
  37. if (percentage <= 35) {
  38. // Create notification
  39. showNotification(percentage);
  40. }
  41. }
  42.  
  43. function showNotification(percentage) {
  44. // Check if Notification API is supported
  45. if ('Notification' in window) {
  46. // Request permission if not already granted
  47. if (Notification.permission !== 'granted') {
  48. Notification.requestPermission();
  49. }
  50.  
  51. if (Notification.permission === 'granted') {
  52. new Notification('Battery Warning', {
  53. body: `Arey azaamu, battery ${percentage}%! maatrame undi, please charging pettu rarey!!`,
  54. icon: 'https://cdn-icons-png.flaticon.com/512/3103/3103446.png'
  55. });
  56. }
  57. }
  58. }
  59.  
  60. // Check battery every 5 minutes as a fallback
  61. setInterval(function() {
  62. navigator.getBattery().then(function(battery) {
  63. checkBatteryLevel(battery.level);
  64. });
  65. }, 300000);
  66. })();