Ticket #723 (closed defect: fixed)

Opened 4 years ago

Last modified 4 years ago

WordPress.com Stats pollute the js namespace - fix proposal

Reported by: gonchuki Owned by: andy
Priority: normal Component: stats
Severity: major Keywords: stats, mootools, clash, javascript, conflict
Cc:

Description

note: previously posted this on wp.org public forum and they directed me to here. I'm opening the ticket here with a little reworded version of the issue as I took a look over the implementation.


I just got struck by an ugly implementation in the  wordpress.com Stats plugin that deliberately adds functions to the global namespace polluting it and clashing against  mootools on the addEvent function, thus breaking the library at least under IE, Safari and Opera (Gecko-based browsers natively implement addEventListener and don't get the method redefined).

For sanity and future proofness, I would suggest you to encapsulate your functions in a private namespace, as you are not only colliding with mootools, but also any other library out there implementing an addEvent function in the global namespace (DOM Assistant comes to mind right now, there may be more)

FWIW I coudn't find the js code in the repository, but I got my hands on the generated code by sniffing the requests under IE. As I see it, it should be simple to solve and I will be submitting a patch as soon as I can (I'm getting horrid errors in my blog and need it fixed ASAP). I will just ask to whoever manages the js file to accept my fixes and upload/commit it to the  http://stats.wordpress.com server.

Attachments

stats_track.js Download (2.4 KB) - added by gonchuki 4 years ago.
stats tracking javascript file
stats.php Download (11.6 KB) - added by gonchuki 4 years ago.
stats.php slightly modified to use the new js

Change History

comment:1 Changed 4 years ago by gonchuki

  • Summary changed from WordPress.com Stats pollute the js namespace to WordPress.com Stats pollute the js namespace - fix proposal

Here's my proposal for fixing the namespace pollution. I took also the liberty to clean the javascript and remove some dirty code (the event handling code was terribly error prone, and there was a lot of duplication), and the result is a much cleaner component.

Now all runs inside a wpcom_stats namespace, and it's been tested under IE6, IE7, Opera 9, Safari 3 win and Firefox.

I also did the pertinent addition to the credits section, and my proposal is to bump the version to 1.2.0 (also updated it in the file, check and correct as needed)

Changed 4 years ago by gonchuki

stats tracking javascript file

Changed 4 years ago by gonchuki

stats.php slightly modified to use the new js

comment:2 Changed 4 years ago by andy

  • Status changed from new to closed
  • Resolution set to fixed

Thanks for all the effort, gonchuki. The plugin rotates the js url weekly as a cache buster, so I'll just edit the js to change the offending function name. Everyone will have the benefit within seven days and nobody will have to update their plugin.

A future version may incorporate the encapsulation you proposed but I don't see that as sufficient cause for a new version of the plugin.

Note: See TracTickets for help on using tickets.