G-play show rating

Shows the rating as text next to the stars in Google Play app listings

  1. // ==UserScript==
  2. // @name G-play show rating
  3. // @description Shows the rating as text next to the stars in Google Play app listings
  4. // @version 2.0.0
  5. // @author wOxxOm
  6. // @namespace wOxxOm.scripts
  7. // @license MIT License
  8. // @match https://play.google.com/*
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13.  
  14. (document.head || document.documentElement)
  15. .appendChild(document.createElement('style')).textContent = /*css*/`
  16. [data-wx-rating]::before {
  17. content: attr(data-wx-rating);
  18. }
  19. [data-wx-rating-5]::before {
  20. color: green;
  21. font-weight: bold;
  22. }
  23. [data-wx-rating-4]::before {
  24. color: green;
  25. }
  26. [data-wx-rating-3]::before {
  27. color: cornflowerblue;
  28. }
  29. [data-wx-rating-2]::before {
  30. color: hotpink;
  31. }
  32. [data-wx-rating-1]::before {
  33. color: red;
  34. }
  35. `;
  36.  
  37. const SEL = [1, 2, 3, 4, 5]
  38. .map(r => `[role="img"]:not([data-wx-rating])[aria-label*="${r}"]`).join(',');
  39. const mo = new MutationObserver(onMutation);
  40. const observe = () => mo.observe(document, {
  41. childList: true,
  42. subtree: true,
  43. attributes: true,
  44. });
  45. observe();
  46.  
  47. async function onMutation(mutations) {
  48. let changed;
  49. for (const { target } of mutations) {
  50. if (target && target.firstElementChild && target.querySelector(SEL)) {
  51. for (const star of target.querySelectorAll(SEL)) {
  52. const r = star.getAttribute('aria-label').match(/\b\d(\.\d)?\b|$/)[0];
  53. if (r) {
  54. if (!changed) changed = mo.disconnect() || 1;
  55. star.setAttribute('data-wx-rating', r);
  56. star.setAttribute('data-wx-rating-' + Math.max(1, r | 0), '');
  57. }
  58. }
  59. }
  60. }
  61. if (changed) observe();
  62. }