callback messages from revBrowser to the stack

Interested adding compiled externals from LiveCode and third parties to your LiveCode projects? This is the place to talk about them.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
sp27
Posts: 135
Joined: Mon May 09, 2011 3:01 pm

callback messages from revBrowser to the stack

Post by sp27 » Fri May 27, 2011 6:01 am

The Dictionary lists three callback messages from revBrowser to the stack: browserClick, browserOver, and browserOut.

Can custom messages be passed from the Web page in revBrowser to the stack? Since I write all the Web pages that will be opened in revBrowser in my stack, I can include any calls there, if revBrowser has hooks for passing them back to the stack.

If I were using Adobe Director and Web Xtra for this project, I would say this in the Web page:

ondoubleclick="window.external.execDirectorScript(MyParameter)"

and a handler in the sprite that holds WebXtra (which is very much like an instance of revBrowser on a LC card) would receive this message and the value of MyParameter.

Can something like that be done in LC?

I posted this request on the user list, but no one has responded. I guess it's not an easy question. I don't have access to the developer list.

Thanks!

sp27

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1236
Joined: Sat Apr 08, 2006 1:10 pm
Location: Zurich
Contact:

Re: callback messages from revBrowser to the stack

Post by BvG » Fri May 27, 2011 3:32 pm

I think there is a way, using javascript and rebrowserSet. Maybe the revbrowserExecuteScript or revBrowsrCallScript functions could be of help?

but beyond that i don't really know, sorry.

Oh right, and messages to use can get lost sometimtes especially if they're worded unclear or hard to answer, just try to rephrase and give more detail about what you already tried, you should get an answer, and if you don't try harder ;)
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

sp27
Posts: 135
Joined: Mon May 09, 2011 3:01 pm

Re: callback messages from revBrowser to the stack

Post by sp27 » Mon May 30, 2011 2:09 am

Thanks to the good folks in the LC community, I'm posting a solution that works for me, plus an idea for further work. Most of this discussion took place on the use-livecode list.

Context: we have an instance of revBrowser in the card, and we can control the contents of the Web page that it is showing. That is, we can add a JavaScript function or a trigger to the HTML source. This can be done, for example, if the HTML source is passed to revBrowser by setting its htmltext property in a handler (displaying a "local" page), or if you download the original source as text from its server and then doctor this source before assigning it to revBrowser for display.

Goal: the user double-clicks a word in the Web page, and this word is sent to the stack so a handler can do something with it--maybe look it up in a database or make decisions based on this word.

Problems:

1) As described in the Dictionary, revBrowser doesn't pass a double-click or any strings associated with it back to the stack.

2) Using the revBrowser "selected" property, or using the string data that may be passed back to the stack by the revBrowserCallScript function will not work with Unicode text; it seems to be restricted to single-byte text and will return a question mark for every double-byte character in the Web page (even if revBrowser displays the double-byte page correctly).

Solution 1

In the Web page, put this trigger in the body tag:

Code: Select all

<body ondblclick="textRange=document.selection.createRange(); selectedWord=textRange.text; if (selectedWord.length!=0) window.location = '#?Sel='+selectedWord;">
When the user double-clicks a word in the Web page, that word becomes selected; the page is reloaded; the URL now contains the name/value pair Sel=TheClickedText.

Place this handler in the card script:

Code: Select all

on browserBeforeNavigate pId,pUrl
   answer pUrl
end browserBeforeNavigate
When the user double-clicks a word in the Web page, you will see the answer box with "about:blank#?Sel=WhateverGotClicked". In your actual handler you can easily extract the clicked word from this (shown below). (The about:blank part is displayed if the URL is a fake, i.e. if you gave revBrowser the HTML text instead of having it fetch it from the Web.)

All that doesn't solve Problem #2: if the word that got clicked is not in ANSI characters, pURL will be all question marks. Even when the Web page has a meta tag that states UTF-8 as its character set, revBrowser will remap all non-ANSI characters to question marks. Here is my Solution #2: add a JavaScript function to your Web page that will turn the double-clicked word (the current selection in the browser) into a list of decimal values for the characters:

Code: Select all

function ReturnSelection() {
   var textRange=document.selection.createRange();
   var selectedWord = textRange.text;
   //we want to return the decimal numbers of all characters in selectedWord (instead of the string itself)

   var locResult = "";
   for (var i=0; i<selectedWord.length; i++) {
      locResult = locResult + "," + selectedWord.substr(i, 1).charCodeAt(0);
   }
   locResult = locResult.substring(1); //this gets rid of the opening comma
   // now locResult is a comma-delimited list (a string) of decimal Unicode numbers for the selected string, e.g. "1040,1045,1055,1078"
   return locResult;
}
The pURL parameter of your Live Code handler browserBeforeNavigate is now a list of decimal numbers, one for each character in the word that the user double-clicked in the Web page. Turn this list into a UTF-16 string and assign it to a field like so:

Code: Select all

on browserBeforeNavigate pId,pUrl
--extract the parameter from the URL
   put the itemDelimiter into locItemDel
   set the itemDelimiter to "="
   put the last item of pURL into locSelectedNums --this is now, e.g. "1040,1045,1055,1078"
   set the itemDelimiter to locItemDel
   
   set useUnicode to true
   
   put the number of items in locSelectedNums into locSelectedTextLen
   put empty into locSelectedText
   repeat with i = 1 to locSelectedTextLen
      put numToChar(item i of locSelectedNums) after locSelectedText
   end repeat
   
   set the unicodeText of field "BrowserSelection" of this card to locSelectedText 
end browserBeforeNavigate
The technique for passing numerical values for Unicode strings can be used with the revBrowserCallScript function as well.

Thanks to Mike B. and jacque for their suggestions that led to all this.

Now, a really powerful alternative was proposed by Todd G. I haven't worked on it yet, but I think it's mind-blowing:

"I start wiht an HTTP server in my stack. revHTTP server takes about 450 lines of xTalk. Once I have revHTTP server running alongside my revBrowser, I can use standard HTML/JavaScript to exchange data back and forth... I can serve pages from my stack to the browser, I can send AJAX requests to my stack and the stack can respond with XML or JSON..."

If you run with this idea, please post!

sp27

Post Reply

Return to “Using Externals”