Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cf = "\n", ef = "},",
- ams_rbibitem = "\\RBibitem", ams_bibitem = "\\Bibitem",
- ams_by = "\\by",
- ams_ed = "\\ed",
- ams_paper = "\\paper",
- ams_byy = "\\byy",
- ams_jour = "\\jour",
- ams_book = "\\book",
- ams_inbook = "\\inbook",
- ams_bookinfo = "\\bookinfo",
- ams_serial = "\\serial",
- ams_edition = "\\edition",
- ams_yr = "\\yr",
- ams_vol = "\\vol",
- ams_issue = "\\issue",
- ams_pages = "\\pages",
- ams_publ = "\\publ",
- ams_publaddr = "\\publaddr",
- ams_lang = "\\lang",
- ams_crossref = "\\crossref",
- ams_mathscinet = "\\mathscinet",
- ams_mathnet = "\\mathnet",
- ams_zmath = "\\zmath",
- ams_adsnasa = "\\adsnasa",
- ams_isis = "\\isi";
- function _tag(ams, bib, val) {
- return {
- "ams" : ams,
- "bib" : bib,
- "val" : val
- };
- };
- var actual;
- var tag_num = 25;
- var tags = new Array(tag_num);
- function init() {
- for (var i = 0; i < tag_num; i++) {
- tags[i] = _tag("", "", "");
- }
- tags[0].ams = "\\RBibitem"; tags[0].bib = "@Article{";
- tags[1].ams = "\\by"; tags[1].bib = "author = {";
- tags[2].ams = "\\ed";
- tags[2].bib = "editor = {";
- tags[3].ams = "\\paper";
- tags[3].bib = "title = {";
- //tags[4].ams = "\\byy";
- //tags[4].bib = "editor = {";
- tags[5].ams = "\\jour";
- tags[5].bib = "journal = {";
- //tags[6].ams = "\\book";
- //tags[6].bib = "\\book";
- tags[7].ams = "\\inbook";
- tags[7].bib = "booktitle = {";
- //tags[8].ams = "\\bookinfo";
- //tags[8].bib = "\\bookinfo";
- //tags[9].ams = "\\serial";
- //tags[9].bib = "\\serial";
- tags[10].ams = "\\edition";
- tags[10].bib = "edition = {";
- tags[11].ams = "\\yr";
- tags[11].bib = "year = {";
- tags[12].ams = "\\vol";
- tags[12].bib = "volume = {";
- tags[13].ams = "\\issue";
- tags[13].bib = "number = ";
- tags[14].ams = "\\pages";
- tags[14].bib = "pages = {";
- tags[15].ams = "\\publ";
- tags[15].bib = "publisher = {";
- tags[16].ams = "\\publaddr";
- tags[16].bib = "address = {";
- tags[17].ams = "\\lang";
- tags[17].bib = "language = {";
- //tags[18].ams = "\\crossref";
- //tags[18].bib = "\\crossref";
- //tags[19].ams = "\\mathscinet";
- //tags[19].bib = "\\mathscinet";
- tags[20].ams = "\\mathnet";
- tags[20].bib = "url = {";
- tags[21].ams = "\\zmath";
- tags[21].bib = "note = {";
- //tags[22].ams = "\\adsnasa";
- //tags[22].bib = "\\adsnasa";
- //tags[23].ams = isis;
- //tags[23].bib = isis;
- tags[24].ams = "\\Bibitem";
- tags[24].bib = "@Article";
- }
- var mathnet_ams_full = [];
- var mathnet_bib_full = [];
- var bib_final;
- function bib_verify(entry) {
- //conflict of name-tags
- }
- function ams_to_bib(entry) {
- if ("undefined" !== entry) {
- var i, carry, pos, found_pos, line_end, curr_ams_tag;
- var ams = entry, bib = "";
- ams = ams.replace(/{/g, ' ').replace(/}/g, ' ').replace(/~/g, '').replace(/--/g, '—').replace(/, /g, ' and ');
- //console.log('ams_to_bib:' + ams);
- init();
- for (i = 0; i < tag_num; i++) {
- curr_ams_tag = tags[i].ams;
- if ("" != curr_ams_tag) {
- found_pos = ams.indexOf(curr_ams_tag);
- if (-1 != found_pos) {
- line_end = ams.indexOf(cf, found_pos);
- if (-1 != line_end) {
- tags[i].value = ams.substring(found_pos + tags[i].ams.length, line_end);
- //console.log(tags[i].bib + tags[i].value + ef + '\n');
- bib += tags[i].bib + tags[i].value + ef + '\n';
- }
- }
- }
- }
- //console.log('}');
- bib += '}\n';
- bib = bib.replace('}', '').replace(/{ /g, '{').replace(/ }/g, '}').replace(/ ,/g, ',');
- bib_final += bib;
- actual++;
- var json_bib_final = JSON.stringify(bib_final);
- console.log(json_bib_final);
- if (actual == 10) {
- console.log('ready to go');
- chrome.tabs.create({
- active: false, url: 'http://www.editpad.org/'
- }, function(tab) {
- chrome.tabs.executeScript(tab.id, {
- code: 'document.getElementById("text").value = ' + json_bib_final,
- }, function(result) {
- if (!result || null == result) {
- alert('Failed to run "save_result" script.\n' + chrome.runtime.lastError.message);
- return;
- }
- alert('SUBMITTED: "' + result[0] + '"');
- chrome.tabs.executeScript(tab.id, {
- code: 'document.getElementById("submitted").click()'
- });
- });
- });
- }
- }
- }
- function request(request_num, url, tagname) {
- var xhr = new XMLHttpRequest();
- var doc = document.implementation.createHTMLDocument("tmp");
- xhr.open("GET", url, true);
- xhr.send();
- xhr.onreadystatechange = function() {
- if (4 == xhr.readyState && 200 == xhr.status) {
- if (null == xhr.responseText)
- console.log('Failed to request');
- doc.documentElement.innerHTML = xhr.responseText;
- //console.log('curr:' + doc.getElementById(tagname).innerText);
- mathnet_ams_full[request_num] = doc.getElementById(tagname).innerText;
- ams_to_bib(mathnet_ams_full[request_num]);
- return doc.getElementById(tagname).innerText;
- }
- }
- return -1;
- }
- function save_result(data) {
- chrome.tabs.create({
- active: false, url: 'http://www.editpad.org/'
- }, function(tab) {
- chrome.tabs.executeScript(tab.id, {
- code: 'document.getElementById("text").value = ' + data,
- }, function(result) {
- if (!result || null == result) {
- alert('Failed to run "save_result" script.\n' + chrome.runtime.lastError.message);
- return;
- }
- alert('SUBMITTED: "' + result[0] + '"');
- chrome.tabs.executeScript(tab.id, {
- code: 'document.getElementById("submitted").click()'
- });
- });
- }
- );
- }
- function get_data(url) {
- var data;
- chrome.tabs.create({
- active: false, url: url
- }, function(tab) {
- chrome.tabs.executeScript(tab.id, {
- code: 'data = document.getElementById("citPaperAMSBIBID").innerText',
- }, function(result) {
- if (!result || null == result) {
- alert('Failed to run "get_data" script.\n' + chrome.runtime.lastError.message);
- return;
- }
- alert('GOT: "' + result[0] + '"');
- console.log('GOT:' + result[0]);
- });
- }
- );
- }
- chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
- console.log(sender.tab.url);
- if ("info" == message.key) {
- console.log('Recideved "' + message.key + '" in background');
- var i, recieved_data = JSON.stringify(message.data);
- //alert('RECIEVED:' + recieved_data);
- save_result(recieved_data);
- var bib_json;
- bib_final = "";
- actual = 0;
- var curr_page;
- var curr_url, mathnet_main_url = "http://mi.mathnet.ru/intf";
- var url2 = "http://www.mathnet.ru/php/archive.phtml?wshow=paper&jrnid=intf&paperid=";
- var url3 = "&option_lang=rus";
- var tagname = "citPaperAMSBIBID";
- if (bib_final != "") {
- bib_json = JSON.stringify(bib_final);
- console.log('!' + bib_json);
- return;
- }
- for (i = 1; i < 11; i++) {
- curr_url = url2 + i + url3;
- curr_page = request(i, curr_url, tagname);
- console.log(bib_final);
- if (i == 10 && "" != bib_final) {
- alert('!');
- bib_json = JSON.stringify(bib_final);
- console.log('!!' + bib_json);
- //save_result(bib_final);
- }
- }
- }
- if ("convert_info" == message.key) {
- //save to storage();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement