WordPress.com Stats Sparkline

Adds a stats sparkline to the WordPress.com admin bar

  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.2.0
  7. // @homepageURL https://gf.qytechs.cn/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-2019, 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, statsUrl) {
  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 = statsUrl;
  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 statsUrl = document.querySelector("#wp-admin-bar-blog a.ab-item").href;
  51. if (!statsUrl) return;
  52.  
  53. var blogDomain = statsUrl.replace(/\/+$/, "").split("/").pop();
  54.  
  55. // target: https://example.wordpress.com/wp-includes/charts/admin-bar-hours-scale.php
  56. var sparklineImageSrc = "//" + blogDomain + "/wp-includes/charts/admin-bar-hours-scale.php";
  57. addSparkline(sparklineImageSrc, statsUrl);
  58. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址