WordPress.com edit post redirects

Redirects the new post page to the classic post page

目前为 2015-11-17 提交的版本,查看 最新版本

  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.4.0
  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. // gather information from URL
  17. var parsedUrl=window.location.pathname.match(/(post|page)(?:\/([^\/]+)(?:\/(\d+|new)?)?)?/);
  18. var postType=parsedUrl[1];
  19. var blogid=parsedUrl[2];
  20. var postid=parsedUrl[3];
  21.  
  22.  
  23. /**
  24. * Initiates the redirect.
  25. */
  26. function redirectToClassic() {
  27. // if no blog specified
  28. if(!blogid) {
  29. guessClassicLink();
  30. }// if
  31. else {
  32. // Redirect to post URL based on API results
  33. // API docs: https://developer.wordpress.com/docs/api/
  34. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/"+blogid, apiRedirect, guessClassicLink);
  35. }//end if
  36. }//end redirectToClassic
  37.  
  38.  
  39. /**
  40. * Handles the API request via AJAX.
  41. * @param path the URL to request. The response should be a JSON object.
  42. * @param callback the function to call on success.
  43. * This function should take in a single JSON object.
  44. * @param fallback the function to call on failure
  45. */
  46. // Based on function by dystroy. From http://stackoverflow.com/a/14388512
  47. function fetchJSONFile(path, callback, fallback) {
  48. var httpRequest = new XMLHttpRequest();
  49. httpRequest.onreadystatechange = function() {
  50. if (httpRequest.readyState === 4) {
  51. if (httpRequest.status === 200) {
  52. var data = JSON.parse(httpRequest.responseText);
  53. if (callback) callback(data);
  54. }//if
  55. else {
  56. if(fallback) fallback();
  57. }//end if
  58. }//end if
  59. };//end onreadystatechange()
  60. httpRequest.open('GET', path);
  61. httpRequest.send();
  62. }//end fetchJSONFile
  63.  
  64.  
  65. /**
  66. * Builds the classic editor link using the specified base.
  67. * The base should not have a trailing slash.
  68. */
  69. function buildClassicLink(base) {
  70. var posturl=base;
  71. //new post
  72. if(postid == "new" || postid == null) {
  73. posturl+="/wp-admin/post-new.php?post_type="+postType;
  74. }//if
  75. //existing post
  76. else {
  77. posturl+="/wp-admin/post.php?post="+postid+"&action=edit";
  78. }//end if
  79.  
  80. return posturl;
  81. }//end buildLink
  82.  
  83. /**
  84. * Guesses the link to the classic editor based on the blog ID, which
  85. * should be the domain if it is not numeric.
  86. */
  87. function guessClassicLink() {
  88. // if the blog ID is not numeric, then it is a domain
  89. if(blogid && isNaN(blogid)) {
  90. // use the blogid to build URL
  91. // and redirect
  92. window.location.replace(buildClassicLink("https://"+blogid));
  93. }//if
  94. else {
  95. scrapeSiteLink();
  96. }//end if
  97. }//end guessClassicLink
  98.  
  99.  
  100. /**
  101. * Scrapes the page for the site link
  102. */
  103. function scrapeSiteLink() {
  104. window.onload=function() {
  105. // FIXME this will not work for Jetpack-enabled sites that have
  106. // different WordPress and site roots
  107. // e.g. site is accessed at http://example.com/,
  108. // but admin at http://example.com/wordpress/wp-admin/
  109. blogurl=document.querySelector(".post-editor__sidebar a.site__content").href;
  110.  
  111. // strip trailing slash
  112. if(blogurl.charAt(blogurl.length-1) === '/') {
  113. blogurl=blogurl.substr(0, blogurl.length-1);
  114. }//end if
  115.  
  116. // in case nothing is returned, don't do anything
  117. if(blogurl) {
  118. window.location.replace(buildClassicLink(blogurl));
  119. }//end if
  120. };//end window.onload
  121. }
  122.  
  123. /**
  124. * Sets up a redirect using the given parsed API data.
  125. * @param data the parsed API results as a JSON object
  126. */
  127. function apiRedirect(data) {
  128. // if not a private blog, redirect using API
  129. if(!data.error) {
  130. //redirect
  131. window.location.replace(buildClassicLink(data.URL));
  132. }//if
  133.  
  134. // else this is a private blog
  135. else {
  136. guessClassicLink();
  137. }//end if
  138. }//end apiRedirect
  139.  
  140. // initiate redirect
  141. redirectToClassic();