[feature request] sending messages to jweb
I can use "maxmessage" to send informations from within [jweb] object (actually from webpage loaded into the object). It's really cool feature.
But sometimes I also need to execute some javascript code located inside the webpage loaded into [jweb]. Is there a possibility to send a message to [jweb] to execute - let's say - javascript method located inside the loaded webpage. Or maybe there is a possibility to add this function to the [jweb] object?
@Yaniki
I've some good news: this is already available. Unfortunately the documentation of the initial Max 7 release doesn't include the details but here is something to get you started:
// Communicating with Max from within jweb
---
In Max 7 jweb uses the Chromium Embedded Framework (CEF) for running websites within a Max box. What's new is that you can communicate with the JavaScript of the shown website. Please note that since CEF is in a seperate process, the communication is inherently asynchronous, and you can't rely on having sent and outputted messages within the same event.
// Receiving messages from Max
---
The bindInlet function enables listening to messages being sent to the jweb object from within the JavaScript of the webpage. In order to do that you attach a callback function to certain messages as shown in the example below:
window.max.bindInlet('doSomething', function () {
/*
* trigger functionality without arguments
*
* message 'doSomething'
*/
});
If you'd like to send messages with arguments to the website simply send a list of space delimited values to the jweb object as shown below. You can either access these values as parameters in your listener callback function or use the array-like arguments object if the amount of parameters might vary.
window.max.bindInlet('doSomethingWithArgs', function (a, b) {
/*
* if a fixed set of parameters doesn't meet
* your needs you can use the array-like arguments
* objects in order to gain access to the
* incoming parameters
*
* message: doSomethingWithArgs 1 2
*/
});
// Sending messages to Max
---
Using the outlet function you can output messages from the JavaScript of the shown webpage to the jweb's first outlet.
// output a string
window.max.outlet('foo');
// output a list
window.max.outlet('foo', 1, 2);
// output contents of array with prepended 'foo' message
var ar = [1, 2, 3, 4];
window.max.outlet.apply(window.max, ['foo'].concat(ar));
Additionally you can send messages to Max using the href attribute of an anchor tag if following a certain but simple syntax. Note that a message send this way will be outputted with a prepended 'maxmessage'.
// Interacting with Max dictionaries
---
Using getDict you can access the contents of a Max dict object from within a website's JavaScript.
var nestedValue;
// access dictionary
window.max.getDict('dictName', function(dict) {
// dict is a JS object, so you can do things like
nestedValue = dict.a;
});
If you like to set the contents of a dictionary within Max you can use the the setDict function to do that.
var obj = {
a : '1',
b : '2',
c : '3'
};
window.max.setDict('dictName', obj);
Dear Florian
This is absolutely great. I have no words to express my gratitude. Thank you very much!
This is such a great feature . Any way to get the best of both and send a message to max from the jweb javascript.
something like:
window.max.message('myReceive','test');
would be nice to send messages to the patcher without having to cable up the outlet of jweb.
Hi, I am curious about this feature. To be honest, I don't understand how I can handle this feature even though I am trying to read on and on. Could you show me any simple patch describing this feature as an example?
Maybe you know the answer to a question I posted a few weeks ago. How can I send raw html (say) into the jweb object to have it rendered? For example, I'd like to use Maxurl to retrieve an html page, then I want to massage it and then I want to render the result.
@DHJDHJDHJ - did you try making your own html that gets the page you want to massage, performs the changes you want and then just load that page in jweb with the read message?
From what I read, on the reference page for jweb, the 'read' message wants a URL (including a filename). That means I would have to retrieve HTML, store it somewhere and then read it in.
I want to be able to send HTML directly into jweb so that I can (for example)
1) Fetch HTML using MaxURL
2) Feed that output into some filters (say)
3) Render the results
I don't want to have to store stuff to a file along the way. In other words, I want to be able to do something like
[sprintf "Hello, there" ]
and then prepend that output with a message that then goes directly into jweb for rendering
Don't think jweb supports that. The method above will work just fine for what you want to do.
if you don't want to do it that way - have text write the file with the changes you want and then have jweb read that file.
guys, I really ask you to please give examples of how sending and receiving jweb data works...
https://www.paweljanicki.jp/projects_maxandp5js_en.html - here is an example (download the zip file linked on the webpage) on messaging between the patcher and webpage loaded into jweb
Thank you YANIKI for your quick response, your examples are excellent, but I'm new to both max and js, it's very difficult for me to understand all the processes. Maybe you could give a specific example of interaction with the code of this page https://www.soundcraft.com/ui-demo/mixer.html, for example, move the first fader or press mute from max or get data from the jweb output...
Hello KONSTANTIN
I'm afraid my answer will be a bit disappointing.
To transfer information between a web page loaded into jweb and the patcher you have to prepare the webpage itself in the right way (put in it JavaScript code that receives messages from the patcher or sends it "outside" jweb [i.e. to the patcher]). There is no simple and universal method to do that with third-party webpages, which do not have these mechanisms implemented by authors.
For third-party webpages pages that do not use any special mechanisms on the server side, it is relatively easy to download a page source with all the necessary files and "customize" it to communicate with Max, but I am also not sure about the legal side of such an action... And, of course it is still based on some knowledge about building websites, especially JavaScript.
There is, although, a built-in "executejavascript" mechanism in jweb, which allows injecting JavaScript code into any webpage loaded "inside" the object and in some cases this mechanism may allow someone else's webpage to be "hacked", but you need to read and understand source code of the webpage you want to modify if you want to do that.