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
example.com 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 https://example.com/*
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 wordpress.com 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 wordpress.com 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:
- Scrape the site URL
- If the scraped URL is a prefix of the current URL, add the sparkline
- Else, if the scraped URL matches the URL returned from the API, add the sparkline
- Else, stop
Now the script runs, and it seems to run fine, until you log out. If you log back in through wordpress.com 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 wordpress.com, 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 wordpress.com; 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.
If you have any questions or find any issues, please leave a comment.