Firefox API to replace text with HTML - Mozilla

This is a discussion on Firefox API to replace text with HTML - Mozilla ; Hi, Is there any Firefox API to replace a word say: "Hello" with something like " Hello "; Thanks, Priyank Bolia http://priyank.co.in...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Firefox API to replace text with HTML

  1. Firefox API to replace text with HTML

    Hi,
    Is there any Firefox API to replace a word say: "Hello" with something like
    "Hello";
    Thanks,
    Priyank Bolia
    http://priyank.co.in

  2. Re: Firefox API to replace text with HTML

    No need to use DOM API.
    Use nsIFormatConverter instead.

    function stripHTMLTags(aText) {
    var formatConverter = Cc["@mozilla.org/widget/htmlformatconverter;1"].
    createInstance(Ci.nsIFormatConverter);
    var oldStr =
    Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    oldStr.data = aText;
    var newStr = { value: null };
    try {
    formatConverter.convert("text/html", oldStr, oldStr.toString().length,
    "text/unicode", newStr, {});
    newStr = newStr.value.QueryInterface(Ci.nsISupportsString);
    return newStr.toString();
    }
    catch (ex) {
    return oldStr.toString();
    }
    }

  3. Re: Firefox API to replace text with HTML

    Sorry, maybe I misunderstood your question.


    > No need to use DOM API.
    > Use nsIFormatConverter instead.
    >
    > function stripHTMLTags(aText) {
    > var formatConverter = Cc["@mozilla.org/widget/htmlformatconverter;1"].
    > createInstance(Ci.nsIFormatConverter);
    > var oldStr =
    > Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    > oldStr.data = aText;
    > var newStr = { value: null };
    > try {
    > formatConverter.convert("text/html", oldStr, oldStr.toString().length,
    > "text/unicode", newStr, {});
    > newStr = newStr.value.QueryInterface(Ci.nsISupportsString);
    > return newStr.toString();
    > }
    > catch (ex) {
    > return oldStr.toString();
    > }
    > }


  4. Re: Firefox API to replace text with HTML

    Basically I have a list of 4200 GRE words, and I want to write an extension,
    that whenever any GRE word is found it has to be highlighted and in the
    tooltip its meaning has to be set.
    If I just blindly replace innerHTML the whole page will be messed.
    If I do dom parsing and replace so many nodes, the firefox crashes.
    This is what I wrote:
    function walk(node)
    {
    var highlightWords =
    GREVocabulary_ext_prefs.getBoolPref(highlightGREWo rds_prefName);
    var highlightColor =
    GREVocabulary_ext_prefs.getCharPref(highlightColor _prefName);
    if(node.childNodes.length > 0)
    {
    for(var i = 0; i < node.childNodes.length; i++)
    {
    if(node.childNodes[i].nodeName != "IMG" &&
    node.childNodes[i].nodeName != "A" && node.childNodes[i].nodeName != "FONT")
    walk(node.childNodes[i]);
    }
    }

    if(node.nodeName == '#text')
    {
    re=/[a-zA-Z][a-zA-Z][a-zA-Z]/;
    if(node.nodeValue.length > 0 && node.nodeValue.match(re) != null)
    {
    for (var word in GREVocabulary)
    {
    if(highlightWords)
    {
    nodeList.push(node);
    }
    }
    }
    }
    }

    function replaceNodeValue()
    {
    for(var i = 0; i < 1; i++)
    {
    for (var word in GREVocabulary)
    {
    if(nodeList[i].nodeValue.indexOf(word) >= 0)
    {
    nodeList[i].nodeValue = nodeList[i].nodeValue.replace(word,
    " title='"+GREVocabulary[word]+"'>"+word+"");
    }
    }
    }
    }



    On Sun, May 11, 2008 at 7:50 PM, Gomita wrote:

    > Sorry, maybe I misunderstood your question.
    >
    >
    > > No need to use DOM API.
    > > Use nsIFormatConverter instead.
    > >
    > > function stripHTMLTags(aText) {
    > > var formatConverter = Cc["@

    > mozilla.org/widget/htmlformatconverter;1"].
    > > createInstance(Ci.nsIFormatConverter);
    > > var oldStr =
    > > Cc["@mozilla.org/supports-string;1

    > "].createInstance(Ci.nsISupportsString);
    > > oldStr.data = aText;
    > > var newStr = { value: null };
    > > try {
    > > formatConverter.convert("text/html", oldStr,

    > oldStr.toString().length,
    > > "text/unicode", newStr, {});
    > > newStr =

    > newStr.value.QueryInterface(Ci.nsISupportsString);
    > > return newStr.toString();
    > > }
    > > catch (ex) {
    > > return oldStr.toString();
    > > }
    > > }

    > _______________________________________________
    > dev-extensions mailing list
    > dev-extensions@lists.mozilla.org
    > https://lists.mozilla.org/listinfo/dev-extensions
    >


+ Reply to Thread