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