ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > FARR Plugins and Aliases

CurrencyConverterMini (plugin) [renamed thread]

(1/4) > >>

Nod5:
edit 080813: thread renamed to "CurrencyConverterMini (plugin)" from "google dollar exchange rate plugin -- sketch" /nod5

While the donationcoder server was down I tried to put together a plugin based on the javascript SDK that takes a number as an input and queries google what that amount of dollars exchanges to in swedish kronor. In the finished plugin that swedish kronor part should of course be customizable to whatever currency the user prefers. Below is the fscript.js I've put together so far. It works, but is a bit messy and lacks some things. To try it, make a copy of the Ninjawords plugin folder (for example) and replace the fscript.js script code with the one below. type "ex ___ " to run it, where ___ is some amount . Example: "ex 50 " . The plugin currently treats "ex 50.10 " and "ex 50,10 " as identical (50 dollars and 10 cents).

This is only a sketch version of the script. I'd love some assistance on a few things:

help1: How do I most easily let the user choose the output currency and make the plugin remember that?

help2: I want the plugin to go through these three steps: First, if there is a cached daily exchange rate, use that to calculate and display output. Second, if not, query google, parse, display output and cache exchange rate. Third, if the google query failed (i.e. offline), see if there's a cached older exchange rate, use that to calculate and display output plus a note that N days old rate was used. Now, how do I best do I best cache the exchange rate through javascript in the plugin? I basically need to read and write to a text file I guess but I don't even know how to do that I'm afraid :o

help3: I used "ex" as alias only because I couldn't get "$" to work (I wanted "$50 " to run the script for example). Is there some way to use "$" as an alias?


// plugin script :
displayname="Google exchange rate";
versionstring="1.01.01";
releasedatestring="5.3.2008";
author="nod5";
updateurl="";
homepageurl="";
shortdescription="Google exchange rate";
longdescription="the plugin queries google how many swedish kronor you get for the inputted amount of US dollars";
advconfigstring="Google exchange rate";
readmestring="Google exchange rate";
iconfilename="favicon.ico";

aliasstr="ex";
regexstr="^ex ((?:\d|\.|\,)*?) $";
scriptregex=/^ex ((?:\d|\.|\,)*?) $/;
regexfilterstr="";
keywordstr="";
scorestr="300";

// type
UNKNOWN=0; FILE=1; FOLDER=2; ALIAS=3; URL=4; PLUGIN=5; CLIP=5;
// Postprocessing
IMMEDIATE_DISPLAY=0; ADDSCORE=1; MATCH_AGAINST_SEARCH=2;
// search state
STOPPED=0; SEARCHING=1;

var pre_string = "{\\rtf1\\ansi\\ansicpg1250\\deff0\\deftab709{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}{\\colortbl ;\\red255\\green0\\blue0;}{\\*\\generator Msftedit 5.41.15.1507;}\\viewkind4\\uc1\\pard\\lang2057\\f0\\fs18 ";
var post_string = "\\par\n}";
var history = "";

function doQuery(querykey,querynokeyword){

      querynokeyword = querynokeyword.replace(/,/g,'.');

  var url = "http://www.google.com/search?hl=en&q="+querynokeyword+"$+in+sek";
  var xmlhttp=new ActiveXObject("MSXML2.XMLHTTP");
  xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4) {
      var string = xmlhttp.responseText;
      string = string.replace(/\\n|\\t/g,'');
      string = string.replace(/<font size=-2> <\/font>/g,'');
      string = string.replace(/.+US(\$ = (?:\d|\.|\,)*?) Swedish kronor.*$/g,'US$1');
      xpos = string.search(/US(\$ = .*)/g);
      string = querynokeyword+" "+string.slice(xpos)+" SEK"
      FARR.setStrValue("statusbar","Define");
      history = string;
      FARR.setStrValue("window.richeditmode",pre_string+history+post_string);
      }
    }
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function onSearchBegin(querykey, explicit, queryraw, querynokeyword) {
  if(!explicit) {
    if(aliasstr.indexOf(querynokeyword)!=-1) {
      FARR.setState(querykey,1);
      FARR.emitResult(querykey,aliasstr, aliasstr, iconfilename,ALIAS,IMMEDIATE_DISPLAY,300);
      FARR.setState(querykey,0);
    }
    return;
  }
  FARR.setStrValue("statusbar","$ to SEK google converter");
    display = pre_string+"Type $ amount and hit \\b space\\b0"+post_string;
  FARR.setStrValue("window.richeditmode",display);
}

function onRegexSearchMatch(querykey,querynokeyword) {
  var match_reg = querynokeyword.match(scriptregex);
  doQuery(querykey,match_reg[1]);
}
--- End quote ---

mouser:
this is a nice idea.  i hope our resident farr javascript sdk experts (chezchboy and ecaradec) can help.  actually the answers to your question might make for some functions that could be useful for future plugins (caching support for example).

$ has a special meaning in regular expressions.  try \$ to match the $ character.

czb:
Hi,
just shortly because I am in Netherlands for one week, so I dont have much time:

As to 1: You can put it in advanced setting, or you can try it similar way how I do it in google dictionary etc.
As to 2: I would recommend you to use JSON since it is very simple. You can read more about it here: http://www.json.org/js.html or you may also use XML. Take a look at delicious plugin how advanced setting is saved into a xml file.

ecaradec:
help2: I want the plugin to go through these three steps: First, if there is a cached daily exchange rate, use that to calculate and display output. Second, if not, query google, parse, display output and cache exchange rate. Third, if the google query failed (i.e. offline), see if there's a cached older exchange rate, use that to calculate and display output plus a note that N days old rate was used. Now, how do I best do I best cache the exchange rate through javascript in the plugin?
--- End quote ---
In the delicious plugin I stored the bookmarks in an xml files.
If you need to read and write text files : search for Scripting.FileSystemObject. It is an activex you can use to read and write files.
If you need to read and write xml files, you can use MSXML.DOMDocument.

help3: I used "ex" as alias only because I couldn't get "$" to work (I wanted "$50 " to run the script for example). Is there some way to use "$" as an alias?
--- End quote ---
You can try \$ as mouser tell you.
You can also set regexstr and scriptregex to empty,  set aliasstr to $, delete the onRegexSearchMatch function and only use the explicit match. You can then use javascript regexes only to match params. Regexes are useful when you need FARR to filter results based on a pattern when you use FARR.emitResult() to report results.

I'm very pleased to see all your work around javascript plugins.

Nod5:
Thanks for the feedback mouser & czechboy & ecaradec!  :Thmbsup:
Here is a new version with these improvements:
- the user chooses the input and output currency (stored in options.xml ). If the input/output phrase works in google, it should work in this plugin.
- the plugin uses google only once after each FARR restart, thereafter it uses the cached exchange rate which means lightning fast results. (I'll look into writing the exchange rate to a file later, but I suspect the current in memory cache is enough for most usage)

code for fscript.js :

--- ---// plugin script :
displayname="Currency Converter Mini";
versionstring="1.02.01";
releasedatestring="11.3.2008";
author="nod5";
updateurl="";
homepageurl="";
shortdescription="Convert from one currency to another via google.";
longdescription="To run the plugin, type \"ex 50 \" but replace 50 with the amount you want converted.\nSet input/output currency in advanced plugin options. (examples: $ £ GBP SEK canadian money )\nTest your input/output first by googling \"1 __ in __\", where _ is your input/output.\nAfter a FARR restart the first conversion uses google. Later conversions use the cached exchange rate.\nThe alias \"ex\" stands for exchange rate. The plugin converts one way, between two fixed currencies.\nFor other conversion needs just use google.";
advconfigstring="Currency Converter Mini";
readmestring="Currency Converter Mini";
iconfilename="favicon.ico";

aliasstr="ex";
regexstr="^ex ((?:\d|\.|\,)*?) $";
scriptregex=/^ex ((?:\d|\.|\,)*?) $/;
regexfilterstr="";
keywordstr="";
scorestr="300";

// type
UNKNOWN=0; FILE=1; FOLDER=2; ALIAS=3; URL=4; PLUGIN=5; CLIP=5;
// Postprocessing
IMMEDIATE_DISPLAY=0; ADDSCORE=1; MATCH_AGAINST_SEARCH=2;
// search state
STOPPED=0; SEARCHING=1;

var pre_string = "{\\rtf1\\ansi\\ansicpg1250\\deff0\\deftab709{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}{\\colortbl ;\\red255\\green0\\blue0;}{\\*\\generator Msftedit 5.41.15.1507;}\\viewkind4\\uc1\\pard\\lang2057\\f0\\fs18 ";
var post_string = "\\par\n}";
var history = "";
var exrate = "";

function doQuery(querykey,querynokeyword){

querynokeyword = querynokeyword.replace(/,/g,'.');

if(exrate == ""){
  var url = "http://www.google.com/search?hl=en&q=1"+myinputcurrency+"+in+"+myoutputcurrency;
  var xmlhttp=new ActiveXObject("MSXML2.XMLHTTP");

  xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4) {
      var string = xmlhttp.responseText;
      string = string.match(/<b>1 .* = (?:\d|\.|\,)*? /g);
      var splitstring = string[0].split("=");
      var exrate = splitstring[1].slice(1,-1);
      string = exrate * querynokeyword;
      string = Math.round(string);
      string = querynokeyword+" "+myinputcurrency+" = "+string+" "+myoutputcurrency;
      FARR.setStrValue("statusbar","Currency Converter Mini");
      FARR.setStrValue("window.richeditmode",pre_string+string+post_string);
      }
    }
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}
else {
var string = exrate * querynokeyword;
string = Math.round(string);
string = querynokeyword+" "+myinputcurrency+" = "+string+" "+myoutputcurrency;
FARR.setStrValue("statusbar","Currency Convert Mini");
FARR.setStrValue("window.richeditmode",pre_string+string+post_string);
}

}

myinputcurrency="";
myoutputcurrency="";
function onOptionsChanged() {
    var options=new ActiveXObject("MSXML2.DOMDocument");
    options.load(currentDirectory+"\\options.xml")
    myinputcurrency=options.selectSingleNode("options/myinputcurrency").getAttribute("value");
    myoutputcurrency=options.selectSingleNode("options/myoutputcurrency").getAttribute("value");
}

function onInit(c) {
    currentDirectory=c;

    var options=new ActiveXObject("MSXML2.DOMDocument");
    if(!options.load(currentDirectory+"\\options.xml") ||
        options.selectSingleNode("options/myinputcurrency").getAttribute("value")=='' ||
        options.selectSingleNode("options/myoutputcurrency").getAttribute("value")=='') {
        options.loadXML("<options>\n"+
                        "<myinputcurrency label='exchange from :' value=''/>\n"+
                        "<myoutputcurrency label='exchange to :' value=''/>\n"+
                        "</options>");

        options.save(currentDirectory+"\\options.xml");
        options=null;
        FARR.showOptions();
    } else {
        onOptionsChanged();
    }
}



function onSearchBegin(querykey, explicit, queryraw, querynokeyword) {
  if(!explicit) {
    if(aliasstr.indexOf(querynokeyword)!=-1) {
      FARR.setState(querykey,1);
      FARR.emitResult(querykey,aliasstr, aliasstr, iconfilename,ALIAS,IMMEDIATE_DISPLAY,300);
      FARR.setState(querykey,0);
    }
    return;
  }
  FARR.setStrValue("statusbar","Currency Converter Mini");
    display = pre_string+"Type amount and hit \\b space\\b0"+post_string;
  FARR.setStrValue("window.richeditmode",display);
}

function onRegexSearchMatch(querykey,querynokeyword) {
  var match_reg = querynokeyword.match(scriptregex);
  doQuery(querykey,match_reg[1]);
}

edit:
- Can I add a title or description to the settings popup? (the one that stores the input/output in options.xml)
- I tried using escaping $ to \$ but still couldn't get it to work as an alias. Are you sure that it is possible in FARR?

Navigation

[0] Message Index

[#] Next page

Go to full version