WordPress.com classic stats

Redirects the new stats page to the classic stats page

当前为 2015-03-16 提交的版本,查看 最新版本

  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 1.0.0
  7. // @homepageURL https://github.com/tpenguinltg/wpcom-stats-redirect.user.js
  8. // @grant none
  9. // @license MPLv2.0; http://mozilla.org/MPL/2.0/
  10. // @copyright 2015, tPenguinLTG (http://tpenguinltg.wordpress.com/)
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. var parsedUrl=window.location.pathname.match(/stats(\/([^\/]*))?/);
  15. var blogDomain=parsedUrl[2];
  16.  
  17. // Function by dystroy. From http://stackoverflow.com/a/14388512
  18. function fetchJSONFile(path, callback) {
  19. var httpRequest = new XMLHttpRequest();
  20. httpRequest.onreadystatechange = function() {
  21. if (httpRequest.readyState === 4) {
  22. if (httpRequest.status === 200) {
  23. var data = JSON.parse(httpRequest.responseText);
  24. if (callback) callback(data);
  25. }//end if
  26. }//end if
  27. };//end onreadystatechange()
  28. httpRequest.open('GET', path);
  29. httpRequest.send();
  30. }//end fetchJSONFile
  31.  
  32.  
  33. // if general stats page (i.e. no domain given)
  34. if(!blogDomain) {
  35. window.location.replace("https://wordpress.com/my-stats/");
  36. }//if
  37. // else stats page for specific blog
  38. else {
  39. // Redirect to post URL based on API results
  40. // API docs: https://developer.wordpress.com/docs/api/
  41. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/"+blogDomain, function(data) {
  42. window.location.replace("https://wordpress.com/my-stats/?blog="+data.ID);
  43. });
  44. }//end if