WordPress.com classic stats

Redirects the new stats page to the classic stats page

当前为 2017-06-23 提交的版本,查看 最新版本

  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.2.1
  7. // @homepageURL https://greasyfork.org/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-2016, tPenguinLTG (http://tpenguinltg.wordpress.com/)
  12. // @run-at document-start
  13. // ==/UserScript==
  14.  
  15. // Function by dystroy. From http://stackoverflow.com/a/14388512
  16. function fetchJSONFile(path, callback, fallback) {
  17. var httpRequest = new XMLHttpRequest();
  18. httpRequest.onreadystatechange = function() {
  19. if (httpRequest.readyState === 4) {
  20. if (httpRequest.status === 200) {
  21. if (callback) callback(JSON.parse(httpRequest.responseText));
  22. } else if (fallback) {
  23. fallback();
  24. }
  25. }
  26. };
  27. httpRequest.open('GET', path);
  28. httpRequest.send();
  29. }
  30.  
  31. function redirectToClassicStats(baseUrl, statsType) {
  32. var query = ["page=stats"];
  33.  
  34. switch (statsType) {
  35. case "day":
  36. query.push("unit=1");
  37. break;
  38. case "week":
  39. query.push("unit=7");
  40. break;
  41. case "month":
  42. query.push("unit=31");
  43. break;
  44. }
  45.  
  46. window.location.replace(baseUrl + '/wp-admin/index.php?' + query.join("&"));
  47. }
  48.  
  49. function doRedirect(uri) {
  50. var parsedUri = uri.match(/stats(?:\/(insights|day|week|month|year))?(?:\/(countryviews|posts))?(?:\/([^\/]*))?/);
  51. var statsType = parsedUri[1];
  52. var viewType = parsedUri[2];
  53. var blogDomain = parsedUri[3];
  54.  
  55. if (blogDomain) {
  56. // Redirect to post URL based on API results
  57. // API docs: https://developer.wordpress.com/docs/api/
  58. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/" + blogDomain,
  59. // attempt to redirect using API
  60. function(data) {
  61. redirectToClassicStats(data.URL, statsType);
  62. },
  63.  
  64. // fallback: attempt to use the blog domain
  65. function() {
  66. // use http instead of https in case the server doesn't support https
  67. // (e.g. for Jetpack sites)
  68. redirectToClassicStats('http://' + blogDomain, statsType);
  69. }
  70. );
  71. } else if (statsType != "insights") {
  72. window.onload = function() {
  73. // construct a stats URI from the user's default blog
  74. var defaultBlogStatsUri = "/stats";
  75. if (statsType) defaultBlogStatsUri += "/" + statsType;
  76. defaultBlogStatsUri += "/" + currentUser.primarySiteSlug;
  77. doRedirect(defaultBlogStatsUri);
  78. };
  79. } else {
  80. window.onload = function() {
  81. // construct an insights URI from the user's default blog
  82. doRedirect("/stats/insights/" + currentUser.primarySiteSlug);
  83. };
  84. }
  85. }
  86.  
  87. // redirect unless new stats is explicitly requested
  88. if (window.location.search.search(/from=wp-admin/) === -1) {
  89. doRedirect(window.location.pathname);
  90. }