unread_counter_handler.js 1002 B

123456789101112131415161718192021222324252627282930313233
  1. class UnreadCounterHandler {
  2. static decrement(n) {
  3. this.updateValue((current) => {
  4. return current - n;
  5. });
  6. }
  7. static increment(n) {
  8. this.updateValue((current) => {
  9. return current + n;
  10. });
  11. }
  12. static updateValue(callback) {
  13. let counterElements = document.querySelectorAll("span.unread-counter");
  14. counterElements.forEach((element) => {
  15. let oldValue = parseInt(element.textContent, 10);
  16. element.innerHTML = callback(oldValue);
  17. });
  18. if (window.location.href.endsWith('/unread')) {
  19. let oldValue = parseInt(document.title.split('(')[1], 10);
  20. let newValue = callback(oldValue);
  21. document.title = document.title.replace(
  22. /(.*?)\(\d+\)(.*?)/,
  23. function (match, prefix, suffix, offset, string) {
  24. return prefix + '(' + newValue + ')' + suffix;
  25. }
  26. );
  27. }
  28. }
  29. }