JSFiddle - React, Tailwind, and code Playground
by lid0
HTML
<input id="wikiLink" style="width:400px" value="http://en.wikipedia.org/wiki/HTTP_cookie#Secure_cookie"></input>
<input id="btn" type="button" value="get" />
<hr>
<div id="result"></div>
JavaScript
/**
Author: Lidlanca
Date: 09/05/2014
**/
$("#btn").click(function (){
r = parseWikipediaLink($("#wikiLink").val());
get_section_id_by_anchor(r.title,r.anchor);
});
/*
http://en.wikipedia.org/w/api.php?action=parse&page=HTTP_cookie&prop=sections&format=json
*/
function get_section_id_by_anchor(title,anchor){
var d = $.ajax({
dataType: "json",
url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?",
data: {page:title, prop:"sections"},
async:true
}).then(function (d){
var section_id = -1;
$.each (d.parse.sections, function (k,v){
if (v.anchor.toLowerCase() == anchor.toLowerCase()){
section_id = (1+k);
}
});
if (section_id ==-1) {
section_id=1;
}
get_section_content(title,section_id);
});
}
//http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=HTTP_cookie&rvprop=content&rvsection=5&format=json&rvparse
function get_section_content(title,section_id){
var d = $.ajax({
dataType: "json",
url: "http://en.wikipedia.org/w/api.php?action=query&format=json&callback=?",
data: { titles: title,
prop: "revisions",
rvprop: "content",
rvsection:section_id,
rvparse:"",
format:"json"
},
async:true
}).then (function (d){
$("#result").html( d.query.pages[ Object.keys(d.query.pages)[0] ].revisions[0]["*"]);
});
}
function parseWikipediaLink(wikiURL){
//dumb way to parse the wikipedia link to extract title and section and some stuff that are not needed.
var re = /(http[s]{0,1}):\/\/(.{0,2}).{0,1}wikipedia.org\/wiki\/(((.+)#(.+))|([^#]+))/g;
var str = wikiURL;
var m;
m = re.exec(str);
console.log (m);
map = {m:0,protocol:1,local:2,title:5,title2:3,anchor:6} //map labels to group index
var title =m[map.title] ||...