Click here to Skip to main content
15,897,519 members
Articles / Programming Languages / Javascript

Communicating from the Browser to a Desktop Application

Rate me:
Please Sign up or sign in to vote.
4.84/5 (28 votes)
17 May 2009CPOL15 min read 141.9K   4.8K   107  
A very simple application that uses text to speech to speak out loud the currently selected block of text in your web browser.
// BrowserSpeak
//
// by Mark Gladding
// Copyright 2009 Tumbywood Software
// http://www.text2go.com
//
// You are free to reuse this code in any commercial or non-commercial work.
//
// A bookmarklet to send a speaktext command to the BrowserSpeak application.

var server = "localhost:60024"; // Change the port number for your app to something unique.
var maxreqlength = 1500; // This is a conservative limit that should work with all browsers.

var selectedText = _getSelectedText();
if(selectedText)
{
    _bufferText(escape(selectedText));
    _speakText();
}
void 0; // Return from bookmarklet, ensuring no result is displayed.

function _getSelectedText()
{
    // Get the current text selection using a cross-browser compatible technique.
    if (window.getSelection) 
        return window.getSelection().toString();
    else if (document.getSelection) 
        return document.getSelection(); 
    else if (document.selection) 
        return document.selection.createRange().text; 
    return null;
}

function _formatCommand(command, args)
{
    // Add a timestamp to ensure the URL is always unique and hence
    // will never be cached by the browser.
    return "http://" + server + "/" + command + "/dummy.gif" + args + "&timestamp=" + new Date().getTime(); 
}

function _speakText()
{
    var image = new Image(1,1); 
    image.onerror = function() { _showerror(); };
    image.src = _formatCommand("speaktext", "?source=" + document.URL); 
}
    
function _bufferText(text)
{
    var clearExisting = "true"; 
    var reqs = Math.floor((text.length + maxreqlength - 1) / maxreqlength);
    for(var i = 0; i < reqs; i++)
    {
        var start = i * maxreqlength;
        var end = Math.min(text.length, start + maxreqlength);
        var image = new Image(1,1); 
        image.onerror = function() { _showerror(); };
        image.src = _formatCommand("buffertext", "?totalreqs=" + reqs + "&req=" + (i + 1) + "&text=" + text.substring(start, end) + "&clear=" + clearExisting); 
        clearExisting = "false";
    }
}

function _showerror() 
{
    // Display the most likely reason for an error 
    alert("BrowserSpeak is not running. You must start BrowserSpeak first."); 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions