WordPress.com Stats Sparkline

Adds a stats sparkline to the WordPress.com admin bar

Verze ze dne 30. 12. 2016. Zobrazit nejnovější verzi.

  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.1.0
  7. // @homepageURL https://greatest.deepsurf.us/en/scripts/26076-wordpress-com-stats-sparkline
  8. // @homepageURL https://github.com/tpenguinltg/wpcom-stats-sparkline
  9. // @grant none
  10. // @license MPLv2.0; http://mozilla.org/MPL/2.0/
  11. // @copyright 2016, tPenguinLTG (http://tpenguinltg.wordpress.com/)
  12. // ==/UserScript==
  13.  
  14. // Function by dystroy. From http://stackoverflow.com/a/14388512
  15. function fetchJSONFile(path, callback, fallback) {
  16. var httpRequest = new XMLHttpRequest();
  17. httpRequest.onreadystatechange = function() {
  18. if (httpRequest.readyState === 4) {
  19. if (httpRequest.status === 200) {
  20. if (callback) callback(JSON.parse(httpRequest.responseText));
  21. } else if (fallback) {
  22. fallback();
  23. }
  24. }
  25. };
  26. httpRequest.open('GET', path);
  27. httpRequest.send();
  28. }
  29.  
  30. function addSparkline(src) {
  31. var sparklineImage = document.createElement("img");
  32. sparklineImage.src = src;
  33. sparklineImage.alt = "Stats";
  34. sparklineImage.title = "Showing site views per hour for the last 48 hours. Click for full Site Stats.";
  35. sparklineImage.style.paddingTop = "4px";
  36. sparklineImage.style.paddingBottom = "4px";
  37.  
  38. var statsLink = document.createElement("a");
  39. statsLink.appendChild(sparklineImage);
  40. statsLink.href = "https://wordpress.com/stats/" + window.location.hostname;
  41. statsLink.className = "ab-item";
  42.  
  43. var menuItem = document.createElement("li");
  44. menuItem.appendChild(statsLink);
  45.  
  46. document.getElementById("wp-admin-bar-root-default").appendChild(menuItem);
  47. }
  48.  
  49. window.onload = function() {
  50. var blogUrlAnchor = document.querySelector("#wp-admin-bar-blog-info a.ab-item");
  51. if (!blogUrlAnchor) return;
  52.  
  53. var scrapedBlogUrl = blogUrlAnchor.href.replace(/\/+$/, "");
  54.  
  55. // scraped: https://example.wordpress.com/wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1
  56. // target: https://example.wordpress.com/wp-includes/charts/admin-bar-hours-scale.php
  57. var sparklineImageSrc = document.querySelector("#wp-admin-bar-blog-stats img").src.replace(/-2x|\?.*/g, "");
  58.  
  59. // only act on sites where the user is a member
  60. if (document.URL.startsWith(scrapedBlogUrl)) {
  61. addSparkline(sparklineImageSrc);
  62. } else {
  63. // check for custom domain
  64. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/" + window.location.hostname,
  65. function(data) {
  66. if (scrapedBlogUrl == data.URL) addSparkline(sparklineImageSrc);
  67. }
  68. );
  69. }
  70. }