$doc.sace() the current doc from an included script in an uiextension

I’d like to $doc.save() the current document from a script within an included page (as a docExtra-uiextension), e.g. (the script is run if a request-parameter is set via a button). This works, if the code is on the page itself, but not if it resides within an included page.

#if ( $request.confirm == "1" )
  #set ( $MyAdditionalContent ="Test")
  #set ( $MyContent = $doc.getContent() )
  #set ( $MyContent = "$MyContent $MyAdditionalContent")
  #set ( $discard = $doc.setContent($MyContent) )
  $doc.save()
#end

Upon further testing, it seems the problem is, that opening a tab in the docExtra section changes the url (adding an anchor #). When pressing the confirm-button and setting the request-parameter to ?confirm=1 the page reloads and the script within the included file in the uiextension isn’t executed.

Is there a way around this, do I have set the request-parameter differently?
Do I have to construct the url with the appended request-parameter to include the anchor to the docExtra-tab?

The example-code is just to illustrate, the actual point would be to parse the content of the page for linked images with external sources, download these, save them as attachments and replace the content with updated links to these attachments.

(Originally asked within this thread)