To look for topic's ID, you may use parseTopic() / parseMessage() functions and refer to $GLOBAL['topic'] variable from there - this would allow to create a non-destructive solution. Putting modifications related to the 3rd party code is not a proper solution - that would mean, when upgrading the forum, you will have to restore all of your changes again, or most likely, that would mean your forum can't be upgraded at all.
In general, this solution would be good let's say, for the topic rating - IF you put the AJAX code on the certain topic's page.
Putting such a lot of codes (20...30...40 or how big amount of topics is displayed) on one page means the script will create a respective amount of mySQL requests (in the case of flat-files mode it could be even more critical to read such amount of files on the most heavily loaded forum page).
As soon as your forum has almost zero visits and not overloaded with users, this solution seems nice - but I'm almost sure, if you would have even 20 users in a second, your server would lie down under very bad experience.