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
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?
Quote
// 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]);
}
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]);
}








Logged




















