javascript
  • greasemonkey
  • 2012-06-29 2 views 1 likes 
    1

    매회 true를 반환하는 메서드로 기본 document.hasFocus를 재정의하는 greasemonkey 용 스크립트를 만들어야합니다. Greasemonkey를 사용하여 함수 (document.hasFocus) 재정의

    다음은 코드 내가 사용하려고 :

    var script = document.createElement('script'); 
    script.type = "text/javascript"; 
    script.innerHTML = 'function document.hasFocus{return true;}'; 
    document.getElementsByTagName('head')[0].appendChild(script); 
    

    을하지만 그것은 작동하지 않습니다 ...

    어떤 생각?

    감사

    +0

    삽입 된 스크립트의 구문을 확인하십시오.'document.hasFocus = function() {return true;}'를 사용해야합니다. 만약 당신이 정말로 짧은 코드를 원한다면'document.hasFocus = function()! 0;'과 같습니다. –

    답변

    1

    구문은 꺼져, 그리고 <head> 태그가되지 않을 수 있습니다. 또한 가능하면 innerHTML을 사용하지 마십시오. 그래서 사용

    var scriptNode   = document.createElement('script'); 
    scriptNode.type   = "text/javascript"; 
    scriptNode.textContent = 'document.hasFocus = function() {return true;}'; 
    var targ    = document.getElementsByTagName ('head')[0] || document.body; 
    targ.appendChild (scriptNode); 
    

    그러나이 경우, unsafeWindow (파이어 폭스 그리스 몽키)를 사용하여 거의 위험이 있습니다. 그래서 전체 스크립트 코드가 될 수있다 :

    unsafeWindow.document.hasFocus = function() {return true;}; 
    

    여전히 "작동하지 않습니다"가 실제 결과 대 예상 (작동하지 않는 방법을 설명하는 경우, 오류 코드; 기타.). 대상 페이지에 링크하십시오.

    +0

    감사 보스, 이제 작동 :) – brazoayeye

    관련 문제