WordPress.com edit post redirects

Redirects the new post page to the classic post page

目前為 2015-03-22 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name WordPress.com edit post redirects
  3. // @namespace tpenguinltg
  4. // @description Redirects the new post page to the classic post page
  5. // @include https://wordpress.com/post*
  6. // @include https://wordpress.com/page*
  7. // @version 1.2.2
  8. // @homepageURL https://greasyfork.org/en/scripts/8581-wordpress-com-edit-post-redirects
  9. // @homepageURL https://github.com/tpenguinltg/wpcom-edit-post-redirect.user.js
  10. // @grant none
  11. // @license MPLv2.0; http://mozilla.org/MPL/2.0/
  12. // @copyright 2015, tPenguinLTG (http://tpenguinltg.wordpress.com/)
  13. // @run-at document-start
  14. // ==/UserScript==
  15.  
  16. // Based on function by dystroy. From http://stackoverflow.com/a/14388512
  17. function fetchJSONFile(path, callback, fallback) {
  18. var httpRequest = new XMLHttpRequest();
  19. httpRequest.onreadystatechange = function() {
  20. if (httpRequest.readyState === 4) {
  21. if (httpRequest.status === 200) {
  22. var data = JSON.parse(httpRequest.responseText);
  23. if (callback) callback(data);
  24. }//if
  25. else {
  26. if(fallback) fallback();
  27. }//end if
  28. }//end if
  29. };//end onreadystatechange()
  30. httpRequest.open('GET', path);
  31. httpRequest.send();
  32. }//end fetchJSONFile
  33.  
  34.  
  35. /**
  36. * Scrapes the loaded page for the link to the classic editor.
  37. */
  38. function scrapeClassicLink() {
  39. // scrape the edit URL from the page when the DOM has finished loading
  40. window.onload=function() {
  41. var classicLink=document.getElementsByClassName("switch-to-classic")[0].children[0].href;
  42. window.location.replace(classicLink);
  43. }; //end window.onload
  44. }//end scrapeClassicLink
  45.  
  46.  
  47. /**
  48. * Sets up a redirect using the given parsed API data.
  49. */
  50. function apiRedirect(data) {
  51. // if not a private blog and is not Jetpack-enabled, redirect using API
  52. if(!data.error && !data.jetpack) {
  53. var postURL;
  54.  
  55. //new post
  56. if(postid == "new") {
  57. postURL=data.URL+"/wp-admin/post-new.php?post_type="+postType;
  58. }//if
  59.  
  60. //existing post
  61. else {
  62. postURL=data.URL+"/wp-admin/post.php?post="+postid+"&action=edit";
  63. }//end if
  64.  
  65. //redirect
  66. window.location.replace(postURL);
  67. }//if
  68.  
  69. // else this is a private blog or is Jetpack-enabled
  70. else {
  71. scrapeClassicLink();
  72. }//end if
  73. }//end apiRedirect
  74.  
  75.  
  76. // start
  77.  
  78. // gather information from URL
  79. var parsedUrl=window.location.pathname.match(/(post|page)(\/(\d+)\/(\d+|new))?/);
  80. var postType=parsedUrl[1];
  81. var blogid=parsedUrl[3];
  82. var postid=parsedUrl[4];
  83.  
  84. // if no blog given
  85. if(!blogid) {
  86. scrapeClassicLink();
  87. }// if
  88. else {
  89. // Redirect to post URL based on API results
  90. // API docs: https://developer.wordpress.com/docs/api/
  91. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/"+blogid, apiRedirect, scrapeClassicLink);
  92. }//end if