WordPress.com classic stats

Redirects the new stats page to the classic stats page

  1. // ==UserScript==
  2. // @name WordPress.com classic stats
  3. // @namespace tpenguinltg
  4. // @description Redirects the new stats page to the classic stats page
  5. // @include https://wordpress.com/stats*
  6. // @version 2.3.0
  7. // @homepageURL https://gf.qytechs.cn/en/scripts/8621-wordpress-com-classic-stats
  8. // @homepageURL https://github.com/tpenguinltg/wpcom-stats-redirect.user.js
  9. // @grant none
  10. // @license MPLv2.0; http://mozilla.org/MPL/2.0/
  11. // @copyright 2015-2017, tPenguinLTG (http://tpenguinltg.wordpress.com/)
  12. // @run-at document-start
  13. // ==/UserScript==
  14.  
  15. var script = {
  16. start: function(path) {
  17. this.doRedirect(this.parseUri(path));
  18. },
  19.  
  20. assembleUrl: function(base, period, query) {
  21. var queryPart = ["page=stats"].concat(query);
  22.  
  23. switch (period) {
  24. case "day":
  25. queryPart.push("unit=1");
  26. break;
  27. case "week":
  28. queryPart.push("unit=7");
  29. break;
  30. case "month":
  31. queryPart.push("unit=31");
  32. break;
  33. }
  34.  
  35. return base + "/wp-admin/index.php?" + queryPart.join("&");
  36. },
  37.  
  38. parseUri: function(uri) {
  39. var parsedUri = uri.match(/stats(?:\/(insights|day|week|month|year))?(?:\/(countryviews|posts))?(?:\/([^\/]*))?/);
  40. return {
  41. statsType: parsedUri[1],
  42. viewType: parsedUri[2],
  43. blogDomain: parsedUri[3],
  44. }
  45. },
  46.  
  47. doRedirect: function(tokens) {
  48. if (tokens.blogDomain) {
  49. // Redirect to post URL based on API results
  50. // API docs: https://developer.wordpress.com/docs/api/
  51. this.utils.apiFetch("/sites/" + tokens.blogDomain,
  52. // attempt to redirect using API
  53. (function(data) {
  54. this.utils.locationReplace(this.assembleUrl(data.URL, tokens.statsType));
  55. }).bind(this),
  56.  
  57. // fallback: attempt to use the blog domain
  58. (function() {
  59. // use http instead of https in case the server doesn't support https
  60. // (e.g. for Jetpack sites)
  61. this.utils.locationReplace(this.assembleUrl("http://" + tokens.blogDomain, tokens.statsType));
  62. }).bind(this)
  63. );
  64. } else if (tokens.statsType != "insights") {
  65. this.utils.registerOnload((function() {
  66. // construct a stats URI from the user's default blog
  67. var defaultBlogStatsUri = "/stats";
  68. if (tokens.statsType) defaultBlogStatsUri += "/" + tokens.statsType;
  69. defaultBlogStatsUri += "/" + currentUser.primarySiteSlug;
  70. this.doRedirect(this.parseUri(defaultBlogStatsUri));
  71. }).bind(this));
  72. } else {
  73. this.utils.registerOnload((function() {
  74. // construct an insights URI from the user's default blog
  75. this.doRedirect(this.parseUri("/stats/insights/" + currentUser.primarySiteSlug));
  76. }).bind(this));
  77. }
  78. },
  79.  
  80.  
  81. utils: {
  82. locationReplace: function(url) {
  83. window.location.replace(url);
  84. },
  85.  
  86. registerOnload: function(onload) {
  87. window.onload = onload;
  88. },
  89.  
  90. // Based on function by dystroy. From http://stackoverflow.com/a/14388512
  91. apiFetch: function(path, callback, fallback) {
  92. var base = "https://public-api.wordpress.com/rest/v1.1";
  93. var httpRequest = new XMLHttpRequest();
  94. httpRequest.onreadystatechange = function() {
  95. if (httpRequest.readyState === 4) {
  96. if (httpRequest.status === 200) {
  97. if (callback) callback(JSON.parse(httpRequest.responseText));
  98. } else if (fallback) {
  99. fallback();
  100. }
  101. }
  102. };
  103. httpRequest.open("GET", base + path);
  104. httpRequest.send();
  105. }
  106. }
  107. };
  108.  
  109. if (typeof module == "object" && module != null) module.exports = script;
  110.  
  111. if (typeof window == "object"
  112. && window.location.search.search(/from=wp-admin/) === -1)
  113. script.start(window.location.pathname);

QingJ © 2025

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