WordPress.com Stats Sparkline

Adds a stats sparkline to the WordPress.com admin bar

As of 2016-12-27. See the latest version.

  1. // ==UserScript==
  2. // @name WordPress.com Stats Sparkline
  3. // @namespace tpenguinltg
  4. // @description Adds a stats sparkline to the WordPress.com admin bar
  5. // @include https://*.wordpress.com/*
  6. // @version 1.0.0
  7. // @homepageURL https://github.com/tpenguinltg/wpcom-stats-sparkline
  8. // @grant none
  9. // @license MPLv2.0; http://mozilla.org/MPL/2.0/
  10. // @copyright 2016, tPenguinLTG (http://tpenguinltg.wordpress.com/)
  11. // ==/UserScript==
  12.  
  13. window.onload = function() {
  14. var blogUrlAnchor = document.querySelector("#wp-admin-bar-blog-info a.ab-item");
  15. if (!blogUrlAnchor) return;
  16.  
  17. var blogUrl = blogUrlAnchor.href;
  18.  
  19. // only act on sites where the user is a member
  20. if (!document.URL.startsWith(blogUrl)) return;
  21.  
  22. var sparklineImage = document.createElement("img");
  23. sparklineImage.src = blogUrl + "/wp-includes/charts/admin-bar-hours-scale.php";
  24. sparklineImage.alt = "Stats";
  25. sparklineImage.title = "Showing site views per hour for the last 48 hours. Click for full Site Stats.";
  26. sparklineImage.style.paddingTop = "4px";
  27. sparklineImage.style.paddingBottom = "4px";
  28.  
  29. var statsLink = document.createElement("a");
  30. statsLink.appendChild(sparklineImage);
  31. statsLink.href = "https://wordpress.com/stats/" + window.location.hostname;
  32. statsLink.className = "ab-item";
  33.  
  34. var menuItem = document.createElement("li");
  35. menuItem.appendChild(statsLink);
  36.  
  37. document.getElementById("wp-admin-bar-root-default").appendChild(menuItem);
  38. }