Chrome 확장 프로그램의 콘텐츠 스크립트로 원격 스크립트를 삽입하려고하는데 Chrome 실행 환경의 어두운 영역 (적어도 나에게)이 영향을 받고있는 것처럼 느껴집니다. .Chrome 확장 프로그램의 콘텐츠 스크립트로 원격 스크립트
jQuery의 $.getScript
을 그 (및 기타) 목적으로 사용하고 싶습니다. 여기
사출 코드 (훌륭하게 suggested here)이다 :
// inject jQuery
chrome.tabs.executeScript(null, { file: "js/jquery.js" }, function() {
// inject remote script with jQuery
chrome.tabs.executeScript(null, { code: '$.getScript("https://mysite.com/myremotescript.js", function(){ })' });
});
여기myremotescript.js
원격 스크립트이다 - 그것은 간단 :
$("body").css("backgroundColor", "green");
오류이다 "$ is not defined"
document.body.style.backgroundColor = "green";
그것은뿐만 아니라 $
정의되지 않은이 있다고 나타납니다 myremotescript.js
가 변경되는 경우가 작동하기 때문에 오류에 언급 된
는 myremotescript.js
기능의 것으로 보인다. 나는에 myremotescript.js
변경하는 경우 :
function Do(){
document.body.style.backgroundColor = "green";
}
을 다음 $.getScript
의 콜백에서 마()를 실행합니다
chrome.tabs.executeScript(null, {
code: '$.getScript("https://mysite.com/myremotescript.js", function(){ Do(); })'
});
오류는 다음과 같습니다 "Do is not defined"
모든 아이디어/설명/제안?
편집 : 해결책 : 롭-W 대답 @에 따라, 그것은했다. $.get()
이 오류가되지 않도록 추가해야하는 유일한 단점은 데이터 형식을 "텍스트"로 표시하는 것이 었습니다.
$.get("https://mysite.com/myremotescript.js", null, null, "text")
.done(function(remoteCode){
chrome.tabs.executeScript(null, { code: remoteCode }, function(){
chrome.tabs.executeScript(null, { code: "Do();" });
});
})
감사합니다. Rob. '$ .get()'을 사용하여 크로스 도메인 xhr을 수행하기 때문에 일치 패턴이있는 사용 권한을 추가해야합니다 (어떤 이유로'$ .getScript()는 필요하지 않습니다). 사실인가요? 확장 설치 중에 사용자에게 특별한 경고를 추가합니까? –
"example.com의 모든 데이터에 액세스 할 수 있습니다". https://developer.chrome.com/extensions/permission_warnings.html을 참조하십시오. –
예 ... 다소 혼란 스럽습니다. 사용자 혼란에 대한 부분입니다. background/page에서'$ .getScript'를 실행하고 나서 '