Extend share button functionality on top of Confluence page

Hello everyone!

I’m quite new to Confluence plugin development and I’m looking for a way to extend the page sharing feature in Confluence Server/Data Center with the help of a plugin.

What I want to do is execute some custom code - in addition to the normal page sharing code - when a user shares a Confluence page with other users using the “Share” button on top of a page.

What would be the best way to do this? The only way I found so far is to inject some JavaScript using web-resource and use this to add an additional click listener to the “Share” button, which then runs my custom code. But this seems like a bad hack to me.

Is this really the way to go or is there a more elegant solution for this?