Skip to content

Instantly share code, notes, and snippets.

@shivprasad
Created August 6, 2009 17:33
Show Gist options
  • Save shivprasad/163454 to your computer and use it in GitHub Desktop.
Save shivprasad/163454 to your computer and use it in GitHub Desktop.
function nseQuotePuller(pblock,args){
var baseUrl = "http://www.nseindia.com/marketinfo/companyinfo/companysearch.jsp";
var params = {cons: Utils.trim(args.object.text),section:7};
jQuery.get( baseUrl, params, function( resp ) {
var begindex=resp.indexOf("/marketinfo/equities/cmquote_tab.jsp");
var searchresult=resp.indexOf("<font class=header3>Search Results</font>");
var noResult = resp.indexOf("<font class=contenttext>Click <a href=javascript:history.back()>here</a> to search again</font>");
if(begindex==-1 && searchresult==-1){
return;
}
if(noResult>0){
pblock.innerHTML=resp.substring(searchresult,noResult);
return;
}
if(searchresult>0){
var searchResponse = resp.substring(searchresult);
searchResponse=searchResponse.substring(0,searchResponse.indexOf("</TABLE>")+8);
searchResponse=searchResponse.replace("<a href=javascript:history.back()>Back</a>","");
searchResponse=searchResponse.replace(/<A /g,"<blah ");
searchResponse=searchResponse.replace(/<\/a>/g,"</blah>");
pblock.innerHTML=searchResponse;
return;
}
var url="http://www.nseindia.com/marketinfo/equities/ajaxGetQuote.jsp?symbol="+parseForSymbol(resp);
fetchQuoteFromNSE(url,pblock);
})
}
function parseForSymbol(resp){
var res = resp.substring(resp.lastIndexOf("getCMOnlineQuoteData('")+22);
res = res.substring(0,res.indexOf("'"));
return res;
}
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function fetchQuoteFromNSE(url,pblock){
jQuery.get(url,null,function(resp){
var a = resp.split(";");
if(trim(a[0])=='SUCCESS')
{
var renderText = "<b>"+a[51]+"</b><br/><br/>";
renderText += "<table border='1' align='left' cellpadding='4' cellspacing='1' width='40%'> <tr> <th class='specialhead' colspan='2'>Price Information</th></tr>";
renderText += "<tr><th class='specialhead2' align='left'>Open</th><td class='t1'>"+a[10]+"</td></tr>";
renderText += "<tr><th class='specialhead2' align='left'>High</th><td class='t1'>"+a[11]+"</td></tr>";
renderText += "<tr><th class='specialhead2' align='left'>Low</th><td class='t1'>"+a[12]+"</td></tr>";
renderText += "<tr><th class='specialhead2' align='left'><b>Last Price</b></th><td class='t1'><b>"+a[14]+"</b></td></tr>";
renderText += "<tr><th class='specialhead2' align='left'>Prev. Close</th><td class='t1'>"+a[9]+"</td></tr>";
renderText += "<tr><th class='specialhead2' align='left'>Change</th><td class='t1'>"+a[15]+"</td></tr>";
renderText += "<tr><th class='specialhead2' align='left'>% Change</th><td class='t1'>"+a[16]+"</td></tr>";
renderText += "<tr><th class='specialhead2' align='left'>52 week high price</th><td class='t1'>"+a[7]+"</td></tr>";
renderText += "<tr><th class='specialhead2' align='left'>52 week low price</th><td class='t1'>"+a[8]+"</td></tr>";
renderText += "</table>";
}
pblock.innerHTML=renderText;
return;
})
}
function nseIndexPuller(pblock){
var baseUrl = "http://www.nseindia.com/marketinfo/indices/indexwatch.jsp";
jQuery.get( baseUrl, null, function( resp ) {
var begindex=resp.indexOf("<TABLE BORDER=\"0\" ALIGN=\"CENTER\" CELLPADDING=\"4\" CELLSPACING=\"1\" width=\"100%\">");
var indexText=resp.substring(begindex);
indexText=indexText.substring(0,indexText.indexOf("</table>")+8);
indexText=indexText.replace(/(<img src=\/images\/grph\.gif border=0>)/g,"");
indexText=indexText.replace(/\/images\//g,"http://www.nseindia.com/images/");
indexText = indexText.replace(/(<a href="([^"]+)">([^<]+)<\/a>)/g, "$3");
pblock.innerHTML=indexText;
})
}
function nseToday(pblock,featuretype){
var url="http://www.nseindia.com/content/equities/NiftySecuritiesTop.htm"
if(featuretype=="losers")
url="http://www.nseindia.com/content/equities/NiftySecuritiesBelow.htm";
if(featuretype=="options")
return nseOptions(pblock);
if(featuretype=="chart")
return nseChart(pblock);
jQuery.get( url, null, function( resp ) {
var begindex=resp.indexOf("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#969696\" width=\"780\">");
var featureText=resp.substring(begindex);
featureText=featureText.substring(0,featureText.indexOf("</table>")+8);
featureText=featureText.replace(/(<a href=([^">]+)>([^<]+)<\/a>)/g, "$3");
featureText=featureText.replace("cellpadding=\"4\"","cellpadding=\"2\"");
pblock.innerHTML=featureText.replace(/#969696/g,"");
})
}
function nseOptions(pblock){
url="http://www.nseindia.com/content/fo/fomktwtch_OPTIDXNIFTY.htm";
jQuery.get( url, null, function( resp ) {
var begindex=resp.indexOf("<TABLE BORDER=\"0\" ALIGN=\"CENTER\" CELLPADDING=\"4\" CELLSPACING=\"1\" width=750>");
var featureText=resp.substring(begindex);
featureText=featureText.substring(0,featureText.indexOf("</table>")+8);
featureText=featureText.replace(/(<a href ="([^">]+)">([^<]+)<\/a>)/g, "$3");
featureText=featureText.replace("CELLPADDING=\"4\" CELLSPACING=\"1\"","");
featureText=featureText.replace(/OPTIDX/g,"");
featureText=featureText.replace(/Instrument Type/g,"");
featureText=featureText.replace(/Underlying/g,"");
featureText=featureText.replace(/NIFTY/g,"");
pblock.innerHTML=featureText;//.replace(/#969696/g,"");
})
}
function nseChart(pblock){
pblock.innerHTML="<img src=http://profit.ndtv.com/Markets/StockImage.aspx?stocktype=I&stock=NSE><br/><br/><img src=http://www.nseindia.com/Watchimage_nifty.png>";
}
CmdUtils.CreateCommand({
names: ["nse"],
arguments: [{role: "object",
nountype: noun_arb_text,
label: "company name"}],
icon: "http://www.nseindia.com/favicon.ico",
help: "<i>nse *name of company*</i> - enter name of company to get quote from NSE.<br/><i>nse</i> - get Index information.<br/> <i>nse gainers</i> - gives you today's gainers.<br/> <i>nse losers</i> - gives you today's losers.",
author: {name: "Shivprasad Bade"},
homepage:"http://byteco.de",
license: "GPL",
preview: function(pblock,args){
var userInput = Utils.trim(args.object.text);
if(userInput.length<2) {
nseIndexPuller(pblock);
return;
}
var commands=["gainers","losers","options","chart"];
if(commands.indexOf(userInput.toLowerCase())>=0){
nseToday(pblock,userInput.toLowerCase());
return;
}
nseQuotePuller(pblock,args);
},
execute: function(arguments) {
if(Utils.trim(arguments.object.text).length<2)
return;
var arg = Utils.trim(arguments.object.text);
var baseUrl = "http://www.nseindia.com/marketinfo/companyinfo/companysearch.jsp?cons="+arg+"&section=7";
Utils.openUrlInBrowser(baseUrl);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment