2012-06-07 12 views
1

내 문제는 Tumblr의 tinymce 편집기의 내용을 사용자 스크립트로 가져 오려고합니다.Chrome의 사용자 스크립트로 tinyMCE에서 콘텐츠 가져 오기

var tm = (unsafeWindow.tinyMCE) ? unsafeWindow.tinyMCE : null; 
if(tm!= null){ 
    var _tempcontent = tm.activeEditor.getContent(); 
    console.log(_tempcontent); 
} 

을하지만 파이어 폭스의 존재에있는 동안 크롬에, 나는 "정의되지 않은"얻을 나는 unsafeWindow을 개정 할 때 다음 내가 TinyMCE에 인스턴스를 찾을 수 없습니다 : 파이어 폭스에서는이 완벽하게 작동합니다.

+0

무엇이 정의되지 않습니까? 변수 tm? var tinymce를 확인할 수 있습니까? 너는 무엇을 얻 느냐? – Thariama

+0

예, unsafeWindow.tinyMCE가 아니기 때문에 변수 tm이 정의되지 않았습니다. – Openmindeo

답변

1

Google 크롬은 이제 사용자 스크립트 it does not allow any access to the target page's javascript objects에 대해 unsafeWindow을 정의합니다. DOM에 대한 액세스 만 허용합니다.

는 크로스 브라우저 스크립트를 제공 할 수 해결하려면, 완전한 기능을 갖춘 unsafeWindow- this answer와 같이

/*--- 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(); 
    })(); 
} 

OR, 당신이 사용하는 스크립트를 리팩토링 수 있습니다 스크립트 삽입 ...

function main() { 
    //--- PUT EVERYTHING IN THIS WRAPPER FUNCTION... 
    var tm = tinyMCE; 
    if (tm!= null) { 
     var _tempcontent = tm.activeEditor.getContent(); 
     console.log(_tempcontent); 
    } 
} 

addJS_Node (null, null, main); 


function addJS_Node (text, s_URL, funcToRun) { 
    var D         = document; 
    var scriptNode       = D.createElement ('script'); 
    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; 
    //--- Don't error check here. if DOM not available, should throw error. 
    targ.appendChild (scriptNode); 
} 
+0

두 가지 옵션은 내가 인터넷을 통해 발견 한 일부 tintmce 데모와 함께 작동하는 것으로 보입니다. 왜냐하면 저는 일하고 프록시 설정으로 tumblr에 들어갈 수 없으므로 집에 올 때 이것을 테스트 할 것입니다. 귀하의 답변에 감사드립니다. – Openmindeo