1

페이지에서 Ajax 호출을 도용하기 위해 XMLHttpRequest.prototype.open 함수에 과부하를 걸기 원하는 Greasemonkey 스크립트를 작성하고 있습니다.GM 스크립트에 @grant를 추가하면 XMLHttpRequest.prototype.open의 오버로드가 손상됩니다.

나는 다음과 같은 코드를 사용하고 있습니다 : I는 GM의 API를 사용하여 시작할 때까지

// ==UserScript== 
// @name  name 
// @namespace namespace 
// @description desc 
// @include  https://url* 
// @version  1.0 
// ==/UserScript== 

if (XMLHttpRequest.prototype) { 
    //New Firefox Versions 
    XMLHttpRequest.prototype.realOpen = XMLHttpRequest.prototype.open; 
    var myOpen = function(method, url, async, user, password) { 

     //call original 
     this.realOpen (method, url, async, user, password); 
     myCode(); 

    } 
    //ensure all XMLHttpRequests use our custom open method 
    XMLHttpRequest.prototype.open = myOpen ; 
} 


이 잘 작동합니다. 난 그냥 메타 섹션 내 코드 나누기를 다음 줄을 추가하고 myOpen가 더 이상 호출되지 않을 때 :

// @grant  GM_getValue 

이는 정말 GM의 API, 내 코드 중단 될 수 있습니다. GM API를 사용해도 내 스크립트의 다른 모든 기능은 정상적으로 작동하지만, 이는 깨지는 기능 인 XMLHttpRequest.prototype.open의 과부하 일뿐입니다.

waitForKeyElements을 사용하여 문제를 해결할 수 있지만 사용하는 간격 때문에 브라우저 속도가 느려지므로 마음에 들지 않습니다.

왜 GM API가 XMLHttpRequest.prototype.open 호출의 과부하를 깰 수 있습니까?

많은 감사,

피터

+0

'waitForKeyElements'가 실제로 페이지 속도를 떨어 뜨리는 경우가 있습니다.보고 싶습니다. (원래 waitForKeyElements 코드를 작성 했으므로 비용 효율적으로 가능한 버그와 성능 문제를 제거하고 싶습니다.) –

답변

0

The @grant directive는 다시 그리스 몽키의 샌드 박스 스위치 -는 GM_의 기능을 사용하는 데 필요한있다.

그런 다음 prototype 오버라이드는 스크립트 범위에만 영향을 미치며 페이지 범위에 영향을 미치기를 원할 것입니다. 이 문제를 해결하려면 을 삽입해야합니다. 그래서 같이 : GM_ 기능을 혼합하여 주입하는 것이

function overrideXhrFunctions() { 
    if (XMLHttpRequest.prototype) { 
     //New Firefox Versions 
     XMLHttpRequest.prototype.realOpen = XMLHttpRequest.prototype.open; 
     var myOpen = function(method, url, async, user, password) { 

      //call original 
      this.realOpen (method, url, async, user, password); 
      myCode(); 
     } 
     //ensure all XMLHttpRequests use our custom open method 
     XMLHttpRequest.prototype.open = myOpen ; 
    } 
} 

addJS_Node (null, null, overrideXhrFunctions) { 

//-- addJS_Node is a standard(ish) function 
function addJS_Node (text, s_URL, funcToRun, runOnLoad) { 
    var D         = document; 
    var scriptNode       = D.createElement ('script'); 
    if (runOnLoad) { 
     scriptNode.addEventListener ("load", runOnLoad, false); 
    } 
    scriptNode.type       = "text/javascript"; 
    if (text)  scriptNode.textContent = text; 
    if (s_URL)  scriptNode.src   = s_URL; 
    if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()'; 
    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement; 
    targ.appendChild (scriptNode); 
} 


주 코드는 까다로운 얻을 수 있습니다. 이를 수행하는 방법 중 하나는 "How to use GM_xmlhttpRequest in Injected Code?"을 참조하십시오.

+0

코드를 삽입 해 주셔서 감사합니다. – pondrejk

+0

안녕하세요. 기꺼이 도와주세요. –

관련 문제