2012-12-02 2 views
0

나는 버튼을 클릭 팝업 창을 여는 크롬 확장 프로그램이 있습니다Chrome 확장 프로그램에서 팝업 창의 URL을 변경할 수 있습니까?

"browser_action": { 
    "default_popup": "popup.html" 
}, 

팝업이로드 될 때, 나는 현재의 URL에 따라, 그것은 다른 URL로 리디렉션하고 싶습니다 활성 브라우저 탭.

나는 팝업에서 window.location.href = "http://example.com/"을 시도했지만 빈 페이지 만 얻었습니다.

전체 팝업 창을 채우는 iframe에서 URL로드를 시도했습니다. 괜찮습니다.하지만 새 URL이 window.close()를 호출 할 때 팝업 창을 닫아야합니다. iframe에있을 때 감지 할 수 있습니다.

이것이 가능합니까?

답변

1

자신이 만든 페이지 인 경우 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 호출 이제 모든 페이지.

관련 문제