자신이 만든 페이지 인 경우 window.postMessage을 사용하여 iframe의 페이지에서 닫을 것을 지시하는 팝업 메시지를 보낼 수 있습니다. iframe이있는
당신이 당신의 파일이 필요합니다 코드는 ...
popup.js
// change #frame to point at your iframe
var frame = document.querySelector('#frame');
window.addEventListener('message', closeWindow, false);
function closeWindow(event) {
if(frame.src.indexOf(event.origin) === 0 && event.data == 'closeWindow') window.close();
}
페이지
window.close = function() {
window.parent.postMessage("closeWindow", window.parent.location.href);
}
는 어떤 페이지를 수행하려면 iframe에서 나는 정말 깨끗한 방법을 생각할 수 없지만 이렇게 할 것입니다.
window.parent의 URL을 확인하여 iframe에 있는지 확인하는 모든 프레임의 모든 URL에서 실행되는 콘텐츠 스크립트가 있어야합니다.
iframe에있는 경우 위에 위와 같이 window.close 이벤트를 수행하십시오.
우리가 타고 싶은 window.close 함수는 페이지가 아닌 콘텐츠 스크립트의 컨텍스트에 있으므로 페이지에 스크립트를 연결하십시오.
여기이 수행하는 코드 ...
의 manifest.json의
{
"name": "PopUp IFrame Parent Window Close",
"description" : "http://stackoverflow.com/questions/13673428/is-it-possible-to-change-the-url-of-the-popup-window-in-a-chrome-extension",
"version": "1.0",
"permissions": [
"tabs", "<all_urls>"
],
"browser_action": {
"default_title": "Test Closing IFrames parent if in popup",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["myscript.js"],
"all_frames":true
}
],
"manifest_version":2
}
popup.html
<html>
<head>
<script type="text/javascript" src='popup.js'></script>
</head>
<body>
<iframe id="frame" src="http://localhost/windowclose.html"></iframe>
</body>
</html>
popup.js
function onLoad() {
// change #frame to point at your iframe
var frame = document.querySelector('#frame');
window.addEventListener('message', closeWindow, false);
function closeWindow(event) {
if(frame.src.indexOf(event.origin) === 0 && event.data == 'closeWindow') window.close();
}
}
window.addEventListener("load", onLoad)
myscript.js
hijackClose = function() {
window.close = function() {
window.parent.postMessage("closeWindow", window.parent.location.href);
}
}
// Executing an anonymous script
function exec(fn) {
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = '(' + fn + ')();';
document.documentElement.appendChild(script); // run the script
document.documentElement.removeChild(script); // clean up
}
if(window.parent && window.parent.location.href == chrome.extension.getURL('popup.html')) {
exec(hijackClose);
}
... 팝업을 닫을 것이다 팝업 iframe에 window.close 호출 이제 모든 페이지.