Chrome에서 Greasemonkey/Firefox의 UnsafeWindow 기능을 어떻게 모방하려면 어떻게해야합니까?

StackOverflow https://stackoverflow.com/questions/1622145

문제

나는 지금 Chrome의 사용자 스크립트를 가지고 다니고 있으므로 잠재적 인 무지/관용을 견딜 수 있습니다.

내가 스크립트를 쓰고있는 페이지에는 <script> 변수를 선언하는 요소 x. 이것은 내 사용자 스크립트에서 액세스 할 수 있음을 의미합니까? x 글로벌 네임 스페이스에서?

예를 들어, 내 Userscript의 유일한 줄이 alert(x);, 예상대로 작동해야합니다 (가정 x 문자열)? Chrome은 UnsafeWindow를 지원하지 않는다는 것을 이해하지만 어떤 이유로 든 기능을 모방하는 방법을 찾는 것이 불가능하다고 생각합니다. 가능합니까?

도움이 되었습니까?

해결책

contentWindow Chrome 3에서 사용할 수 있었지만 크롬 4에서 제거되었습니다. 크롬 4에 대한 가능한 솔루션 :

location.href="javascript:(function(){ alert('Hello'); })()"

다른 팁

이것은 당신에게 창 객체에 대한 참조를 제공합니다 (p).

var p = unsafeWindow;

if(window.navigator.vendor.match(/Google/)) {
    var div = document.createElement("div");
    div.setAttribute("onclick", "return window;");
    p = div.onclick();
};

업데이트:
그만큼 onclick Exploit은 더 이상 최신 크롬 릴리스에서 작동하지 않습니다.

얻기 위해 unsafeWindow Chrome의 기능은 가장 좋은 방법은 설치 및 사용입니다. 탬퍼 몬키 - 당신이 똑똑 할 것입니다. TamperMonkey는 Greasemonkey API와 훨씬 쉬운 스크립트 관리를 완전히 지원합니다.

Greasemonkey 스크립트 및 탬퍼 몬키 스크립트는 거의 항상 완전히 호환되며 일반 크롬 사용자 스크립트에는 해당되지 않습니다.

탬퍼 몬키 (Tampermonkey)를 포기하는 유일한 대안은 어떤 형태의 형태를 사용하는 것입니다. 스크립트 주입.



다음은 이제 쓸모가 없습니다.

크롬은 이제 정의합니다 unsafeWindow 사용자 스크립트 / 콘텐츠 스크립트 용, 그러나 크롬 unsafeWindow 여전히 대상 페이지에서 생성 된 JS 객체에 대한 액세스를 허용하지 않습니다.

다음은 제대로 안전하지 않은 것을 제공하는 방법입니다. unsafeWindow -크로스 브라우저 방식으로 사용합니다 기능 감지 (좋은) 대 브라우저 스니핑 (나쁜):

/*--- Create a proper unsafeWindow object on browsers where it doesn't exist
    (Chrome, mainly).
    Chrome now defines unsafeWindow, but does not give it the same access to
    a page's javascript that a properly unsafe, unsafeWindow has.
    This code remedies that.
*/
var bGreasemonkeyServiceDefined     = false;

try {
    if (typeof Components.interfaces.gmIGreasemonkeyService === "object") {
        bGreasemonkeyServiceDefined = true;
    }
}
catch (err) {
    //Ignore.
}

if ( typeof unsafeWindow === "undefined"  ||  ! bGreasemonkeyServiceDefined) {
    unsafeWindow    = ( function () {
        var dummyElem   = document.createElement('p');
        dummyElem.setAttribute ('onclick', 'return window;');
        return dummyElem.onclick ();
    } ) ();
}

Page JavaScript와 상호 작용하려면 페이지에 스크립트를 삽입해야합니다. (물론이 페이지에서 제안 된 핵을 사용하고 싶지 않다면) 나는 내 자신의 스크립트를 위해 그 기능을 수행 할 수있는 기능을 요리했습니다. 누구나 사용하려는 경우 여기에 게시하겠습니다.

/*
    @description    This function will insert the given code as a <script> or <style> block into a page.
    @param The code to insert; supported types are: JavaScript Function, String (JavaScript), String (CSS).
    @param2 Optional: The type of code that is inserted. If omitted, "js" is assumed. Possible values are 'js' or 'css'.
    @return The HTML element that was inserted, or FALSE on failure
*/
function insert(z,t){
    var j,f,x,c,i,n,d
    d=document
    c=d.createElement
    i=d.head.appendChild
    a=d.createTextNode
    if(typeof z==='function') j=!0,f=!0;
    if((t=='js'||!t)&&!f){j=!0,f=!1}
    if(t=='css'&&!j){x=c('style');x.setAttribute('type','text/css')}
    if(j){x=c('script');x.setAttribute('type','text/javascript')}
    if(f) n=a('('+z+')()');else n=a(z)
    x.appendChild(n)

    if(x){return i(x)}else{return !1}
}

명확히하기위한 몇 가지 예 :

//Inserting a JavaScript function
var func=function(){
    stopAds();
    startFileDownload();
}

insert(func);


//Inserting JavaScript as a string
var strJS="prompt(\"Copy:\",someVariableAtThePage);";

insert(strJS);
//Or with an OPTIONAL 2nd parameter:
insert(strJS,'js');


//Inserting CSS
var strCSS=".ad{display:none !important}    #downloadButton{display:block}";

insert(strCSS,'css');//Specifying 2nd parameter as "css" is required.

OK는 주소 표시 줄을 사용하여 스크립트를 주입 할 수있는 아이디어입니다 ...

javascript:var ElEm = document.createElement("script");ElEm.src='[path_to_script]';document.body.appendChild(ElEm);

그런 다음 JavaScript로 창에서 원하는 것을 실행할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top