Custom domain support added to stats sparkline script

My stats sparkline script has been updated to v1.1.0! This version adds support for custom domains.

If you have auto-update enabled for the script, you should receive the update soon. Otherwise, you can force an update check for the script in your extension or head over to Greasy Fork and install it manually.

If you own a custom domain, you have an extra step to do, again because of technical limitations. By default, the script won’t run on your custom domain because it doesn’t know about it. To fix that, find the script in your user scripts extension (Greasemonkey, Tampermonkey, etc.), go to the script’s options and add the following as a user include, where is your custom domain:*

If your extension doesn’t support user includes, you will have to edit the script manually, although doing this means you won’t get automatic updates and you will have to do this every time you update. If you need to this, add the following line after the existing @include line:

// @include*

A big shout-out goes to Dennis for alerting me of the bug and helping me test! This release couldn’t have happened without him.

If you’d like to read about the technical details of the update, continue reading. Otherwise, if you have any questions or experience any issues, please leave a comment. Happy blogging!

The main issue addressed by this release is that the script would not run in the wp-admin pages for blogs with custom domains. After some thought, I realized that the URL scraped from the page was not a prefix of the current URL; the scraped URL would contain the custom domain, and the current URL was a one.

I had a hunch that the scraped URL would match the URL returned from an API call, and indeed it does, except for a final trailing slash. Thankfully, the API allows a domain to be used to look up the details of a site with a custom domain. So, to determine if the script should run, the new logic is as follows:

  1. Scrape the site URL
  2. If the scraped URL is a prefix of the current URL, add the sparkline
  3. Else, if the scraped URL matches the URL returned from the API, add the sparkline
  4. Else, stop

Now the script runs, and it seems to run fine, until you log out. If you log back in through without visiting your site, the image doesn’t appear until you visit your custom domain. As expected, the image src was built from the scraped site link, which contains the custom domain, and there lies the problem: until you visit your custom domain, there is no cookie set on that domain to tell it that you’re logged in. Consequently, even though you’re logged in on, your custom domain thinks you’re not logged in because it doesn’t have a cookie to say that you are, and it can’t access cookies set for; because it thinks you’re not logged in, it blocks access to the stats image.

How do we fix that? There’s already a sparkline in the Master Bar, so why not use that? I didn’t want to do that at first because scraping it would leave the script vulnerable to layout changes, but it seems to be the most reliable option now. This change is also shared with non-custom domains.

And so, to support custom domains, match the scraped URL against the URL returned from the API, and modify the existing sparkline image src instead of constructing it.

The source code is available on GitHub. You can also view the code for v1.1.0.

If you have any questions or find any issues, please leave a comment.

1 comment

`$name' says...

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: