Chrome userscript error: "Unsafe JavaScript attempt to access frame"

// the iframe of the div I need to access
var iframe = document.getElementsByTagName("iframe")[2];
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

// resize 'player' in the iframe
innerDoc.getElementById('player').width = "1000px";
innerDoc.getElementById('player').height = "650px";

Run in usercript for this URL: http://www.free-tv-video-online.me/player/sockshare.php?id=24DA6EAA2561FD60

Why does Chrome exit with this error and the script does not work ?:

Unsafe JavaScript attempt to access frame with URL http://www.sockshare.com/embed/24DA6EAA2561FD60 
from frame with URL http://www.free-tv-video-online.me/player/sockshare.php?id=24DA6EAA2561FD60. 
Domains, protocols and ports must match.

(I'm just the main Javascript user)


Final code, many thanks to the respondent:

// ==UserScript==
// @name       Resize
// @include    http://www.free-tv-video-online.me/player/sockshare.php*
// @include    http://www.sockshare.com/*
// ==/UserScript==

if (!(window.top === window.self)) {
    var player = document.getElementById('player');
    setSize(player);
}

function setSize(player) {
    player.style.setProperty("width", "1000px");
    player.style.setProperty("height", "650px");
}
+4
source share
2 answers

It is true that regular javascript cannot access the contents of the iframe in another domain for security reasons. However , this in no way stops user scripts in Chrome, Tampermonkey or Greasemonkey.

iframed- , Chrome ( Firefox) iframe' , . , .

, , domain_A.com:

<html>
<body>
    <iframe src="http://domain_B.com/SomePage.htm"></iframe>
</body>
</html>


@match :

// @match http://domain_A.com/*
// @match http://domain_B.com/*

script - iframe, .

, script :

// ==UserScript==
// @name  _Test iFrame processing in Chrome and Tampermonkey
// @match http://domain_A.com/*
// @match http://domain_B.com/*
// ==/UserScript==

if (/domain_A\.com/i.test (document.location.href) ) {
    //Main page
    document.body.style.setProperty ("background", "lime", "important");
}
else {
    //iFrame
    document.body.style.setProperty ("background", "pink", "important");
}

-, iframed - .


, :

if (window.top === window.self) {
    //--- Code to run when page is the main site...
}
else {
    //--- Code to run when page is in an iframe...
}




( ), Chrome. !. , . "" , , , .

+13

javascript iframe .

. :

jQuery iframe-

+1

All Articles