G-play show rating

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

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name          G-play show rating
// @description   Shows the rating as text next to the stars in Google Play app listings
// @version       2.0.0
// @author        wOxxOm
// @namespace     wOxxOm.scripts
// @license       MIT License
// @match         https://play.google.com/*
// @run-at        document-start
// @grant         none
// ==/UserScript==


(document.head || document.documentElement)
  .appendChild(document.createElement('style')).textContent = /*css*/`
  [data-wx-rating]::before {
    content: attr(data-wx-rating);
  }
  [data-wx-rating-5]::before {
    color: green;
    font-weight: bold;
  }
  [data-wx-rating-4]::before {
    color: green;
  }
  [data-wx-rating-3]::before {
    color: cornflowerblue;
  }
  [data-wx-rating-2]::before {
    color: hotpink;
  }
  [data-wx-rating-1]::before {
    color: red;
  }
`;

const SEL = [1, 2, 3, 4, 5]
  .map(r => `[role="img"]:not([data-wx-rating])[aria-label*="${r}"]`).join(',');
const mo = new MutationObserver(onMutation);
const observe = () => mo.observe(document, {
  childList: true,
  subtree: true,
  attributes: true,
});
observe();

async function onMutation(mutations) {
  let changed;
  for (const { target } of mutations) {
    if (target && target.firstElementChild && target.querySelector(SEL)) {
      for (const star of target.querySelectorAll(SEL)) {
        const r = star.getAttribute('aria-label').match(/\b\d(\.\d)?\b|$/)[0];
        if (r) {
          if (!changed) changed = mo.disconnect() || 1;
          star.setAttribute('data-wx-rating', r);
          star.setAttribute('data-wx-rating-' + Math.max(1, r | 0), '');
        }
      }
    }
  }
  if (changed) observe();
}