WordPress.com classic stats

Redirects the new stats page to the classic stats page

当前为 2016-12-26 提交的版本,查看 最新版本

  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.1.0
  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))?(?:\/([^\/]*))?/);
  51. var statsType = parsedUri[1];
  52. var blogDomain = parsedUri[2];
  53.  
  54. if (blogDomain) {
  55. // Redirect to post URL based on API results
  56. // API docs: https://developer.wordpress.com/docs/api/
  57. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/" + blogDomain,
  58. // attempt to redirect using API
  59. function(data) {
  60. redirectToClassicStats(data.URL, statsType);
  61. },
  62.  
  63. // fallback: attempt to use the blog domain
  64. function() {
  65. // use http instead of https in case the server doesn't support https
  66. // (e.g. for Jetpack sites)
  67. redirectToClassicStats('http://' + blogDomain, statsType);
  68. }
  69. );
  70. } else if (statsType != "insights") {
  71. window.onload = function() {
  72. // the first blog listed is the user's default blog
  73. var defaultBlogStatsUrl = document.querySelector("a.is-card-link").href;
  74. doRedirect(defaultBlogStatsUrl);
  75. }
  76. } else {
  77. window.onload = function() {
  78. // insights page; get the domain and construct an insights URI
  79. blogDomain = document.querySelector(".stats-tab a").href
  80. doRedirect("/stats/insights/" + blogDomain);
  81. }
  82. }
  83. }
  84.  
  85. doRedirect(window.location.pathname);