Redirect script bug fixed!

I’ve fixed the bug I found yesterday! The current version of the script is now v.1.3.1; you should be updated to it automatically, or you can reinstall it yourself to get the current version.

The bookmarklet has also been updated. Either make a new bookmark at the workaround summary list or update it to this code:

javascript:(function(){var p=location.pathname.match(/(post|page)(\/(\d+)\/(\d+|new))?/);location.replace((p[4]=='new')?(jQuery('.site.blog-select-click.is-selected').attr('data-blogurl')+'/wp-admin/post-new.php?post_type='+p[1]):jQuery('.switch-to-classic>a').attr('href'))})()

Versions 1.2.x of the script searched the page for this bit of code and would redirect to {classic-edit-link}:

<div class="switch-to-classic">
	Tired of waiting? <a href="{classic-edit-link}">Edit this post from your blog's dashboard</a>.</a>
</div>

However, when creating a new post, {classic-edit-link} is https://wordpress.com/post/1/new/. Since this becomes a redirect to the new editor, you end up with an infinite loop of redirects.

The new editor uses the API to do its stuff, so I tried to figure out how to authenticate using the API cookie like the ones in the page do. I couldn’t figure it out, so I fell back to scraping the page again.

I somehow thought to look at the code sidebar. I found this:

<div class="site blog-select-click is-selected" data-blogurl="{blogurl}" data-blogid="{blogid}">

Importantly, the is-selected class only shows up for the currently-selected blog. Perfect, I can get the blog URL by selecting this node and build the URL to redirect to. Thankfully, the editor exposes a jQuery object through the jQuery identifier, so extracting the URL was really easy:

jQuery(".site.blog-select-click.is-selected").attr("data-blogurl")

Since scraping the page the old way is only an issue with creating new posts, I only do the new way when creating a new post because the old way is less prone to failure. The fallback page scraping function now looks like this:

/**
 * Scrapes the loaded page for the link to the classic editor.
 */
function scrapeClassicLink() {
  // scrape the edit URL from the page when the DOM has finished loading
  window.onload=function() {
    var classicLink="";

    //new post
    if(postid == "new" || !blogid) {
      var blogurl=jQuery(".site.blog-select-click.is-selected").attr("data-blogurl");
      classicLink=blogurl+"/wp-admin/post-new.php?post_type="+postType;
    }//if

    //existing post
    else {
      classicLink=jQuery(".switch-to-classic>a").attr("href");
    }//end if

    window.location.replace(classicLink);
  }; //end window.onload
}//end scrapeClassicLink

If you happen to find any more bugs, please report them.

Advertisements
3 comments
  1. I saw the script updating in my browser and was thinking you work quickly :)

    • Thanks. I don’t like broken software, especially if it’s written by me and is used by more than a few people, so I just had to go and fix it. Let me know if you come across with any issues with the update!

`$name' says...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: