Advertisement
Ladies_Man

CX Backgr working

Jul 25th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cf = "\n", ef = "},",
  2. ams_rbibitem = "\\RBibitem",    ams_bibitem = "\\Bibitem",
  3. ams_by = "\\by",
  4. ams_ed = "\\ed",
  5. ams_paper = "\\paper",
  6. ams_byy = "\\byy",
  7. ams_jour = "\\jour",
  8. ams_book = "\\book",
  9. ams_inbook = "\\inbook",
  10. ams_bookinfo = "\\bookinfo",
  11. ams_serial = "\\serial",
  12. ams_edition = "\\edition",
  13. ams_yr = "\\yr",
  14. ams_vol = "\\vol",
  15. ams_issue = "\\issue",
  16. ams_pages = "\\pages",
  17. ams_publ = "\\publ",
  18. ams_publaddr = "\\publaddr",
  19. ams_lang = "\\lang",
  20. ams_crossref = "\\crossref",
  21. ams_mathscinet = "\\mathscinet",
  22. ams_mathnet = "\\mathnet",
  23. ams_zmath = "\\zmath",
  24. ams_adsnasa = "\\adsnasa",
  25. ams_isis = "\\isi";
  26.  
  27. function _tag(ams, bib, val) {
  28.     return {
  29.         "ams" : ams,
  30.         "bib" : bib,
  31.         "val" : val
  32.     };
  33. };
  34.  
  35. var actual;
  36. var tag_num = 25;
  37. var tags = new Array(tag_num);
  38.  
  39. function init() {
  40.     for (var i = 0; i < tag_num; i++) {
  41.         tags[i] = _tag("", "", "");
  42.     }
  43.     tags[0].ams = "\\RBibitem";     tags[0].bib = "@Article{";
  44.     tags[1].ams = "\\by";               tags[1].bib = "author = {";
  45.     tags[2].ams = "\\ed";
  46.     tags[2].bib = "editor = {";
  47.     tags[3].ams = "\\paper";
  48.     tags[3].bib = "title = {";
  49.     //tags[4].ams = "\\byy";
  50.     //tags[4].bib = "editor = {";
  51.     tags[5].ams = "\\jour";
  52.     tags[5].bib = "journal = {";
  53.     //tags[6].ams = "\\book";
  54.     //tags[6].bib = "\\book";
  55.     tags[7].ams = "\\inbook";
  56.     tags[7].bib = "booktitle = {";
  57.     //tags[8].ams = "\\bookinfo";
  58.     //tags[8].bib = "\\bookinfo";
  59.     //tags[9].ams = "\\serial";
  60.     //tags[9].bib = "\\serial";
  61.     tags[10].ams = "\\edition";
  62.     tags[10].bib = "edition = {";
  63.     tags[11].ams = "\\yr";
  64.     tags[11].bib = "year = {";
  65.     tags[12].ams = "\\vol";
  66.     tags[12].bib = "volume = {";
  67.     tags[13].ams = "\\issue";
  68.     tags[13].bib = "number = ";
  69.     tags[14].ams = "\\pages";
  70.     tags[14].bib = "pages = {";
  71.     tags[15].ams = "\\publ";
  72.     tags[15].bib = "publisher = {";
  73.     tags[16].ams = "\\publaddr";
  74.     tags[16].bib = "address = {";
  75.     tags[17].ams = "\\lang";
  76.     tags[17].bib = "language = {";
  77.     //tags[18].ams = "\\crossref";
  78.     //tags[18].bib = "\\crossref";
  79.     //tags[19].ams = "\\mathscinet";
  80.     //tags[19].bib = "\\mathscinet";
  81.     tags[20].ams = "\\mathnet";
  82.     tags[20].bib = "url = {";
  83.     tags[21].ams = "\\zmath";
  84.     tags[21].bib = "note = {";
  85.     //tags[22].ams = "\\adsnasa";
  86.     //tags[22].bib = "\\adsnasa";
  87.     //tags[23].ams = isis;
  88.     //tags[23].bib = isis;
  89.     tags[24].ams = "\\Bibitem";
  90.     tags[24].bib = "@Article";
  91. }
  92.  
  93. var mathnet_ams_full = [];
  94. var mathnet_bib_full = [];
  95. var bib_final;
  96.  
  97. function bib_verify(entry) {
  98.     //conflict of name-tags
  99. }
  100.  
  101. function ams_to_bib(entry) {
  102.     if ("undefined" !== entry) {
  103.         var i, carry, pos, found_pos, line_end, curr_ams_tag;
  104.         var ams = entry, bib = "";
  105.        
  106.         ams = ams.replace(/{/g, ' ').replace(/}/g, ' ').replace(/~/g, '').replace(/--/g, '—').replace(/, /g, ' and ');
  107.        
  108.         //console.log('ams_to_bib:' + ams);
  109.        
  110.         init();
  111.        
  112.         for (i = 0; i < tag_num; i++) {
  113.             curr_ams_tag = tags[i].ams;
  114.             if ("" != curr_ams_tag) {
  115.                 found_pos = ams.indexOf(curr_ams_tag);
  116.                 if (-1 != found_pos) {
  117.                     line_end = ams.indexOf(cf, found_pos);
  118.                     if (-1 != line_end) {
  119.                         tags[i].value = ams.substring(found_pos + tags[i].ams.length, line_end);
  120.                         //console.log(tags[i].bib + tags[i].value + ef + '\n');
  121.                         bib += tags[i].bib + tags[i].value + ef + '\n';
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.         //console.log('}');
  127.         bib += '}\n';
  128.         bib = bib.replace('}', '').replace(/{ /g, '{').replace(/ }/g, '}').replace(/ ,/g, ',');
  129.         bib_final += bib;
  130.         actual++;
  131.         var json_bib_final = JSON.stringify(bib_final);
  132.         console.log(json_bib_final);
  133.         if (actual == 10) {
  134.             console.log('ready to go');
  135.             chrome.tabs.create({
  136.             active: false, url: 'http://www.editpad.org/'
  137.             }, function(tab) {
  138.                 chrome.tabs.executeScript(tab.id, {
  139.                     code: 'document.getElementById("text").value = ' + json_bib_final,
  140.                 }, function(result) {
  141.                     if (!result || null == result) {
  142.                         alert('Failed to run "save_result" script.\n' + chrome.runtime.lastError.message);
  143.                         return;
  144.                     }
  145.                     alert('SUBMITTED: "' + result[0] + '"');
  146.                    
  147.                     chrome.tabs.executeScript(tab.id, {
  148.                         code: 'document.getElementById("submitted").click()'
  149.                     });
  150.                 });
  151.             });
  152.         }
  153.     }
  154. }
  155.  
  156. function request(request_num, url, tagname) {
  157.     var xhr = new XMLHttpRequest();
  158.     var doc = document.implementation.createHTMLDocument("tmp");
  159.    
  160.     xhr.open("GET", url, true);
  161.     xhr.send();
  162.     xhr.onreadystatechange = function() {
  163.         if (4 == xhr.readyState && 200 == xhr.status) {
  164.             if (null == xhr.responseText)
  165.                 console.log('Failed to request');
  166.             doc.documentElement.innerHTML = xhr.responseText;
  167.             //console.log('curr:' + doc.getElementById(tagname).innerText);
  168.             mathnet_ams_full[request_num] = doc.getElementById(tagname).innerText;
  169.             ams_to_bib(mathnet_ams_full[request_num]);
  170.             return doc.getElementById(tagname).innerText;
  171.         }
  172.     }
  173.    
  174.     return -1;
  175. }
  176.  
  177. function save_result(data) {
  178.     chrome.tabs.create({
  179.             active: false, url: 'http://www.editpad.org/'
  180.         }, function(tab) {
  181.             chrome.tabs.executeScript(tab.id, {
  182.                 code: 'document.getElementById("text").value = ' + data,
  183.             }, function(result) {
  184.                 if (!result || null == result) {
  185.                     alert('Failed to run "save_result" script.\n' + chrome.runtime.lastError.message);
  186.                     return;
  187.                 }
  188.                 alert('SUBMITTED: "' + result[0] + '"');
  189.                
  190.                 chrome.tabs.executeScript(tab.id, {
  191.                     code: 'document.getElementById("submitted").click()'
  192.                 });
  193.             });
  194.         }
  195.     );
  196. }
  197.  
  198. function get_data(url) {
  199.     var data;
  200.     chrome.tabs.create({
  201.             active: false, url: url
  202.         }, function(tab) {
  203.             chrome.tabs.executeScript(tab.id, {
  204.                 code: 'data = document.getElementById("citPaperAMSBIBID").innerText',
  205.             }, function(result) {
  206.                 if (!result || null == result) {
  207.                     alert('Failed to run "get_data" script.\n' + chrome.runtime.lastError.message);
  208.                     return;
  209.                 }
  210.                 alert('GOT: "' + result[0] + '"');
  211.                 console.log('GOT:' + result[0]);
  212.             });
  213.         }
  214.     );
  215. }
  216.  
  217. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  218.     console.log(sender.tab.url);
  219.     if ("info" == message.key) {
  220.         console.log('Recideved "' + message.key + '" in background');
  221.        
  222.         var i, recieved_data = JSON.stringify(message.data);
  223.        
  224.         //alert('RECIEVED:' + recieved_data);
  225.         save_result(recieved_data);
  226.         var bib_json;
  227.         bib_final = "";
  228.         actual = 0;
  229.         var curr_page;
  230.         var curr_url, mathnet_main_url = "http://mi.mathnet.ru/intf";
  231.         var url2 = "http://www.mathnet.ru/php/archive.phtml?wshow=paper&jrnid=intf&paperid=";
  232.         var url3 = "&option_lang=rus";
  233.         var tagname = "citPaperAMSBIBID";
  234.        
  235.         if (bib_final != "") {
  236.             bib_json = JSON.stringify(bib_final);
  237.             console.log('!' + bib_json);
  238.             return;
  239.         }
  240.        
  241.         for (i = 1; i < 11; i++) {
  242.             curr_url = url2 + i + url3;
  243.             curr_page = request(i, curr_url, tagname);
  244.             console.log(bib_final);
  245.             if (i == 10 && "" != bib_final) {
  246.                 alert('!');
  247.                 bib_json = JSON.stringify(bib_final);
  248.                 console.log('!!' + bib_json);
  249.                 //save_result(bib_final);
  250.             }
  251.         }
  252.        
  253.        
  254.     }
  255.     if ("convert_info" == message.key) {
  256.         //save to storage();
  257.     }
  258. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement