2014-09-28 19 views
-1

我正在爲VK評論自動化編寫greacemonkey userscript。Greasemonkey unsafeWindow Vk

並不明白爲什麼我無法訪問對象。

VK窗口OBJ有兩個不同的作曲作曲家對象(相同的詞語,但是第一資本更低)。從unsafeWindow作曲家是可訪問的,但低級資源對象作曲家返回未定義。

(function (window, undefined) { 
    ..... 
    some code 
    finding post, openning comment form, pasting text 
    need to call: 
    unsafeWindow.composer.addMedia.checkMessageURLs("Comment text<br />http://example.com",true); 
    ..... 
    console.log(unsafeWindow.Composer); // return object 
    console.log(unsafeWindow.composer); // return undefined 
} 

如果直接在瀏覽器控制檯中運行

composer.addMedia.checkMessageURLs("Comment text<br />http://example.com",true); 

- 一切ok。

任何想法?

回答

1

composer(小寫字母)是在Greasemonkey腳本運行很久之後按需創建的。

要從腳本訪問它,您需要等待它。例如:

var composerChkTmr = setInterval (doStuffWith_composer, 222); 

function doStuffWith_composer() { 
    if (typeof unsafeWindow.composer === "undefined") 
     return; 

    clearInterval (composerChkTmr); 

    // DO WHATEVER, WITH unsafeWindow.composer HERE. 
} 
+0

謝謝!有用! – Dmitry 2014-10-11 06:07:42

+0

不客氣,樂意效勞! – 2014-10-11 06:42:10