3

Basically, when I run this script in Chrome console, I want to get current url, open a new tab, and set it as a value of a textbox.

javascript:(function(){
    var url=location.href;
    var newtab=window.open('http://www.theyoump3.com/');
    newtab.document.getElementsByName('url')[0].value=url;
})();

When I run the command I get exception in console:

Uncaught DOMException: Blocked a frame with origin "https://www.youtube.com" from accessing a cross-origin frame.

Its understood, the CORS problem. Is there a workaround? Passing a url parameter is not supported by that site.

Same problem occurs when trying this via iframe

var f=document.createElement('iframe');
f.src='https://www.youtube.com/watch?v=4J2zo7ArHnw';
f.style="position:absolute;width:400px;height:400px;z-index:99999;border:2px solid black";
document.body.appendChild(f);

of course it will work then src point to the same origin

monstro
  • 6,254
  • 10
  • 65
  • 111
  • The problem is cross domain as its explained https://stackoverflow.com/a/23927967/2894798, It prevents request from one domain to another. – Renzo Calla Aug 04 '17 at 12:39
  • Yes, its CORS, I was wondering if there is any workaround? – monstro Aug 04 '17 at 12:41
  • Are you trying to write an extension? (Should you be?) – Josh Lee Aug 04 '17 at 12:41
  • a bookmarklet, but if writing an extension will work, then I can do an extension, but I am not sure what could be the difference that will solve the CORS issue – monstro Aug 04 '17 at 12:42

2 Answers2

2

If your starting point isn't the same origin as http://www.theyoump3.com/, you won't be able to do this because the JavaScript code you run in the console runs in the context and origin of the page the console is attached to.

If you are doing it from a page on that origin, you just have to wait for the load event:

javascript:(function(){
    var url=location.href;
    var newtab=window.open('http://www.theyoump3.com/');
    newtab.addEventListener("load", function() {
        newtab.document.getElementsByName('url')[0].value=url;
    });
})();

But again: Only if you're starting out on that origin, which I'm guessing you aren't.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    @monstro: Nope. But perhaps two bookmarklets: One to grab the URL to the clipboard and open the new tab, the other to run on the new tab to fill in the input from the clipboard. – T.J. Crowder Aug 04 '17 at 12:45
-1

Use Window.postMessage

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Im sure it will helpfull

spl1nter
  • 9
  • 1