WordPress.com classic stats

Redirects the new stats page to the classic stats page

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

  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.0.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. var parsedUrl = window.location.pathname.match(/stats(?:\/(insights|day|week|month|year))?(?:\/([^\/]*))?/);
  16. var statsType = parsedUrl[1];
  17. var blogDomain = parsedUrl[2];
  18.  
  19. // Function by dystroy. From http://stackoverflow.com/a/14388512
  20. function fetchJSONFile(path, callback, fallback) {
  21. var httpRequest = new XMLHttpRequest();
  22. httpRequest.onreadystatechange = function() {
  23. if (httpRequest.readyState === 4) {
  24. if (httpRequest.status === 200) {
  25. if (callback) callback(JSON.parse(httpRequest.responseText));
  26. } else if (fallback) {
  27. fallback();
  28. }
  29. }
  30. };
  31. httpRequest.open('GET', path);
  32. httpRequest.send();
  33. }
  34.  
  35. function redirectToClassicStats(baseUrl) {
  36. var query = ["page=stats"];
  37.  
  38. switch (statsType) {
  39. case "day":
  40. query.push("unit=1");
  41. break;
  42. case "week":
  43. query.push("unit=7");
  44. break;
  45. case "month":
  46. query.push("unit=31");
  47. break;
  48. }
  49.  
  50. window.location.replace(baseUrl + '/wp-admin/index.php?' + query.join("&"));
  51. }
  52.  
  53. if (blogDomain) {
  54. // Redirect to post URL based on API results
  55. // API docs: https://developer.wordpress.com/docs/api/
  56. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/" + blogDomain,
  57. // attempt to redirect using API
  58. function(data) {
  59. redirectToClassicStats(data.URL);
  60. },
  61.  
  62. // fallback: attempt to use the blog domain
  63. function() {
  64. // use http instead of https in case the server doesn't support https
  65. // (e.g. for Jetpack sites)
  66. redirectToClassicStats('http://' + blogDomain);
  67. }
  68. );
  69. }