-1

I'm coding greacemonkey userscript for Vk comment automation.

And can't understand why I cant access to object.

Vk window obj have two different Composer and composer objects (the same words, but first capital lower). From unsafeWindow Composer is accessable, but lowercapital object composer return undefined.

(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
}

if run

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

directly in browser console - all ok.

Any ideas?

Dmitry
  • 112
  • 1
  • 8

1 Answers1

1

composer (lowercase) is created on demand, long after the Greasemonkey script runs.

To access it from the script, you need to wait for it. Something like:

var composerChkTmr  = setInterval (doStuffWith_composer, 222);

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

    clearInterval (composerChkTmr);

    // DO WHATEVER, WITH unsafeWindow.composer HERE.
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295