We need to write jquery sometimes in phtml file for special requirement. For that you need to define jquery in require function in script tag for using jquery within phtml file.Put below script in phtml file and replace console.log with your custom code.
Replacing some specific string from url is somewhat tricky job we need to do in our day to day life in IT field for making something dynamic. Here is the working solution you can try given below. Above given solution is tried and found working. If you are stuck somewhere then feel free to comment
Set interval is very important method, which is simultaneously checks for the element if the element is exists or not. It is invoke to the time you provided in interval function, And it will continue to invoke till then the element is not found.