1

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?

  • In most cases, you can't change (or even access) content of cross domain page ... `Here is what I got so far` - and what does that do? – Jaromanda X Mar 28 '18 at 00:24
  • Why do you need to repoen the page instead of just writing straight into it? – Patrick Gunderson Mar 28 '18 at 00:27
  • Check out [this question](https://stackoverflow.com/questions/49078807/js-a-method-to-set-cookie-then-load-page) and comments there. I think it's possible to solve your issue by means of some extra tool only. For example: 'iMacros for Chrome'. – Shugar Mar 28 '18 at 11:03
  • @JaromandaX, the code was a simple test to change url and inject html into the new one. Doesn't seem to be a cross origin issue. I already tried to do this with pages of same sub-domain. Didn't worked too. – Peter White Apr 02 '18 at 01:12
  • @PatrickGunderson, I was hoping that other internal users would click the bookmark shortcut (as always do), but instead of open the plain website version, an enhanced one would arise. The way you suggested would imply an extra click: a bookmark shortcut to a specific page and a bookmarklet to boost it. Not a big problem for me, but I can't speak for the others. – Peter White Apr 02 '18 at 01:32
  • @Shugar, thank you for the suggestion! I indeed know iMacros, but its version for Chrome is rather limited. Perhaps I could write my own Chrome extension to do this very simple stuff. – Peter White Apr 02 '18 at 01:41
  • @Peter, it's alright. As far as I know, 'iMacros for Chrome' is not limited to play the bookmarklet code. So only two lines are necessary to solve the problem: `URL GOTO=https://stackoverflow.com/` and `URL GOTO=javascript:document.write("PageBooster3000");`. – Shugar Apr 04 '18 at 13:11

1 Answers1

0

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