问题
Half of my question was answered with this.
I need to open another url (or replace location/href/src, whatever) in the current tab and after that inject HTML via document.write (or innerHTML, whatever) into the new page.
The code will be used in a Chrome bookmarklet which will open an enhanced version of the new page.
Here is what I got so far:
javascript: window.open('https://stackoverflow.com/','_self').document.write('Page Booster 3000');
Any thoughts are appreciated.
P.S 1: the code partially works if the url parameter is empty (''), but it remains in the old page domain.
P.S 2: any way of achieving this in plain vanilla JS?
回答1:
I've reading some threads and just found that it is neither a document.write issue nor a cross origin limitation.
I will take what gion_13 wrote in one of the reference threads:
"it won't work. new page = new script. can't run a script in a different page that it's declared in."
References:
Javascript bookmarklet go to URL and execute
Load a page and apply javascript in a single bookmark
tampermonkey script stops working if I change the page
来源:https://stackoverflow.com/questions/49523946/open-url-in-same-tab-and-document-write-after