triggering button press with javascript

Integration questions/issues
Post Reply
robertotomas
Posts: 26
Joined: Thu Aug 31, 2017 4:05 pm

triggering button press with javascript

Post by robertotomas » Fri Sep 29, 2017 1:41 pm

given the helloworld plugin:

Code: Select all

(function(window, undefined){

    window.Asc.plugin.init = function()
    {
        var sScript = 'var oDocument = Api.GetDocument();';
        sScript += 'var oParagraph = Api.CreateParagraph();';
        sScript += 'oParagraph.AddText(\'Hello world!\');';
        sScript += 'oDocument.InsertContent([oParagraph]);';
        window.Asc.plugin.info.recalculate = true;
        this.executeCommand("close", sScript);
    };

    window.Asc.plugin.button = function(id)
    {
    };

})(window, undefined);
How can I trigger this plugin's button press from the outer window containing the docEditor?

this is may have to be done with a postMessage and adding a listener in my plugin or editing the api.js file .. however, from there I must actually trigger the plugin to fire off. How is that done?

I want to use this to trigger an effect from the UI ..

Maxim
Posts: 1751
Joined: Tue Oct 11, 2016 2:34 pm

Re: triggering button press with javascript

Post by Maxim » Thu Oct 12, 2017 7:13 am

Hello!
You can use this plugin.

robertotomas
Posts: 26
Joined: Thu Aug 31, 2017 4:05 pm

Re: triggering button press with javascript

Post by robertotomas » Thu Oct 12, 2017 7:23 pm

Maxim, I have a plugin that works, very similar in many ways to that one. You don't have to worry about this at this point (I could still use help understanding why our server configuration doesnt seem to recongize plugins at all though).

For my custom plugin to work, I needed to alter app.js.

I can't get *your* plugin to work at all. executing the code in test.js directly in the (otherwise) working docker install on my local dev machine does nothing.

Maxim
Posts: 1751
Joined: Tue Oct 11, 2016 2:34 pm

Re: triggering button press with javascript

Post by Maxim » Tue Oct 17, 2017 5:59 am

Hello robertotomas!
In that case i do not worry :D :D

John at O5
Posts: 3
Joined: Thu Dec 07, 2017 12:31 am

Re: triggering button press with javascript

Post by John at O5 » Thu Dec 07, 2017 12:34 am

robertotomas, can you please tell us what you needed to alter in app.js to get this solved?

robertotomas
Posts: 26
Joined: Thu Aug 31, 2017 4:05 pm

Re: triggering button press with javascript

Post by robertotomas » Thu Dec 07, 2017 7:06 pm

Note tht there was a plugin that Maxim mentioned earlier which was designed to do this. In the particular version I was using when I built this, it did not work. It may work now though.

I just added a listener to curry messages in to the plugin, like:

Code: Select all

;
window.addEventListener("message", receiveMessage, {captive: true, passive: true})

function receiveMessage (event) {
    console.log(window.Asc, event)
	let o
	try {
		o = JSON.parse(event.data)
	} catch (e) {
		console.warn(e)
		o = {}
	}
	o.raw = event.data

	if(o.type !== 'addTag')
		return
		
	o.tag? window.Asc.addTag = o.tag: triggerAddTag()	
}

function triggerAddTag () { 
	$('.item-plugins').click()
}
jQuery is already installed in the app, so there's that if you you need it (like the handy last line above).

Then you just mount onlyoffice kinda like this:

Code: Select all

<div class=onlyoffice-container><div id=onlyoffice>{{ messages.ooloading }}</div></div>
And message it like:

Code: Select all

document.querySelector('.onlyoffice-container iframe').contentWindow.postMessage(JSON.stringify({type: 'addTag', tag: 'bing'}), '*')

John at O5
Posts: 3
Joined: Thu Dec 07, 2017 12:31 am

Re: triggering button press with javascript

Post by John at O5 » Mon Dec 11, 2017 3:49 pm

thank you!

Post Reply