Advertisement
Ladies_Man

BibTexR CX

Jul 30th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //MAINFEST.JSON
  2. {
  3.     "manifest_version": 2,
  4.     "name": "BibTexR",
  5.     "version": "1.6",
  6.     "description": "Easiest way to handle your BibTex entries. Available in early access (BibTexR 1.6)",
  7.     "browser_action": {
  8.         "default_icon": "images/icon_new.png",
  9.         "default_title": "BibTexR",
  10.         "default_popup": "popup.html"
  11.     },
  12.     "icons": {
  13.         "48":   "images/icon_new_48.png",
  14.         "123":  "images/icon_new_128.png"
  15.     },
  16.     "background": {
  17.         "scripts":      ["jquery-2.1.4.js", "background.js"],
  18.         "persistent":   false
  19.     },
  20.     "content_scripts": [
  21.         {
  22.             "matches":  ["<all_urls>"],
  23.             "js":       ["jquery-2.1.4.js", "content.js"]
  24.         }
  25.     ],
  26.     "permissions": [
  27.         "tabs",
  28.         "activeTab",
  29.         "<all_urls>",
  30.         "history"
  31.     ]
  32. }
  33.  
  34.  
  35.  
  36. //POPUP.HTML
  37. <!DOCTYPE html>
  38.  
  39. <html>
  40.   <head>
  41.  
  42.     <title>Extension's Popup</title>
  43.     <link href="style.css" rel="stylesheet">
  44.    <script src="popup.js"></script>
  45.    
  46.  </head>
  47.  <body>
  48.  
  49.     <input id="bar" type="text" placeholder="I'm Feeling Lucky">
  50.    
  51.     <div id="cs_menu" class="menu">
  52.         <div id="cs_search_button"><span>Open Entries</span></div>
  53.         <div id="cs_get_button"><span>Craft Entries</span></div>
  54.     </div>
  55.     <div id="math_menu" class="menu">  
  56.         <div id="math_open_button"><span>Open MathNet</span></div>
  57.         <div id="math_get_button"><span>Request Data</span></div>
  58.     </div>
  59.  
  60.  </body>
  61. </html>
  62.  
  63.  
  64.  
  65. //POPUP.JS
  66.  
  67. document.addEventListener('DOMContentLoaded', function() {
  68.     var i;
  69.     document.getElementById("cs_search_button").addEventListener("click", function() {
  70.         var cs_query = document.getElementsByTagName('input')[0].value;
  71.         if ("" == cs_query) {
  72.             alert('Insert query for entries, please.');
  73.         } else {
  74.             chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
  75.                 chrome.tabs.create({
  76.                     active: true,
  77.                     url: "http://liinwww.ira.uka.de/csbib?query=" + cs_query + "&sort=score&results=bibtex"
  78.                 }, function(tab) {});
  79.             });
  80.         }
  81.     });
  82.     document.getElementById("cs_get_button").addEventListener("click", function() {
  83.         chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
  84.             chrome.tabs.sendMessage(tabs[0].id, {
  85.                 key: "get_cs",
  86.                 tagname: "bibtex"
  87.             });
  88.         });
  89.     });
  90.    
  91.     document.getElementById("math_open_button").addEventListener("click", function() {
  92.         chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
  93.             chrome.tabs.create({
  94.                 active: true,
  95.                 url: "http://www.mathnet.ru/ej.phtml?option_lang=rus"
  96.             }, function(tab) {});
  97.         });
  98.     });
  99.     document.getElementById("math_get_button").addEventListener("click", function() {
  100.         chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
  101.             chrome.tabs.sendMessage(tabs[0].id, {
  102.                 key: "get_math",
  103.                 tagname: "citPaperAMSBIBID"
  104.             });
  105.         });
  106.     });
  107. });
  108.  
  109.  
  110.  
  111. //BACKGROUND.JS
  112. function _tag(ams, bib, val) {
  113.     return {
  114.         "ams" : ams,
  115.         "bib" : bib,
  116.         "val" : val
  117.     };
  118. };
  119.  
  120. var tag_num = 25;
  121. var tags = new Array(tag_num);
  122. var namespace = new Array();
  123. var bib_final, actual;
  124. var cf = "\n", ef = "},";
  125.  
  126. function init() {
  127.     for (var i = 0; i < tag_num; i++) {
  128.         tags[i] = _tag("", "", "");
  129.     }
  130.     tags[0].ams = "\\RBibitem";         tags[0].bib = "@Article{";
  131.     tags[1].ams = "\\by";               tags[1].bib = "author = {";
  132.     tags[2].ams = "\\ed";               tags[2].bib = "editor = {";
  133.     tags[3].ams = "\\paper";            tags[3].bib = "title = {";
  134.     //tags[4].ams = "\\byy";            //tags[4].bib = "editor = {";
  135.     tags[5].ams = "\\jour";             tags[5].bib = "journal = {";
  136.     //tags[6].ams = "\\book";           //tags[6].bib = "\\book";
  137.     tags[7].ams = "\\inbook";           tags[7].bib = "booktitle = {";
  138.     //tags[8].ams = "\\bookinfo";       //tags[8].bib = "\\bookinfo";
  139.     //tags[9].ams = "\\serial";         //tags[9].bib = "\\serial";
  140.     tags[10].ams = "\\edition";         tags[10].bib = "edition = {";
  141.     tags[11].ams = "\\yr";              tags[11].bib = "year = {";
  142.     tags[12].ams = "\\vol";             tags[12].bib = "volume = {";
  143.     tags[13].ams = "\\issue";           tags[13].bib = "number = ";
  144.     tags[14].ams = "\\pages";           tags[14].bib = "pages = {";
  145.     tags[15].ams = "\\publ";            tags[15].bib = "publisher = {";
  146.     tags[16].ams = "\\publaddr";        tags[16].bib = "address = {";
  147.     tags[17].ams = "\\lang";            tags[17].bib = "language = {";
  148.     //tags[18].ams = "\\crossref";      //tags[18].bib = "\\crossref";
  149.     //tags[19].ams = "\\mathscinet";    //tags[19].bib = "\\mathscinet";
  150.     tags[20].ams = "\\mathnet";         tags[20].bib = "url = {";
  151.     tags[21].ams = "\\zmath";           tags[21].bib = "note = {";
  152.     //tags[22].ams = "\\adsnasa";       //tags[22].bib = "\\adsnasa";
  153.     //tags[23].ams = isis;              //tags[23].bib = isis;
  154.     tags[24].ams = "\\Bibitem";         tags[24].bib = "@Article";
  155. }
  156.  
  157. function submit(url, data) {
  158.     chrome.tabs.create({
  159.             active: false,
  160.             url: url
  161.         }, function(tab) {
  162.             if (-1 != url.indexOf('pastebin.com')) {
  163.                 chrome.tabs.executeScript(tab.id, {
  164.                         code: 'document.getElementById("paste_code").value = ' + data,
  165.                     }, function(result) {
  166.                         if (!result || null == result) {
  167.                             alert('Failed to run "save" script.\n' + chrome.runtime.lastError.message);
  168.                             return;
  169.                         }
  170.                         console.log('Submitted:' + result[0]);
  171.                     }
  172.                 );
  173.             }
  174.             if (-1 != url.indexOf('editpad.org')) {
  175.                 chrome.tabs.executeScript(tab.id, {
  176.                         code: 'document.getElementById("text").value = ' + data,
  177.                     }, function(result) {
  178.                         if (!result || null == result) {
  179.                             alert('Failed to run "save" script.\n' + chrome.runtime.lastError.message);
  180.                             return;
  181.                         }
  182.                         console.log('Submitted:' + result[0]);
  183.                         chrome.tabs.executeScript(tab.id, {
  184.                             code: 'document.getElementById("submitted").click()'
  185.                         });
  186.                     }
  187.                 );
  188.             }
  189.         }
  190.     );
  191. }
  192.  
  193. function ams_to_bib(entry) {
  194.     if ("undefined" !== entry) {
  195.         var i, rand_part, carry, pos, found_pos, line_end, curr_ams_tag;
  196.         var ams = entry, bib = "";
  197.        
  198.         console.log('[' + actual + ']');
  199.         console.log(ams);
  200.         ams = ams.replace(/{/g, ' ').replace(/}/g, '').replace(/~/g, ' ').replace(/--/g, '—').replace(/, /g, ' and ');
  201.        
  202.         for (i = 0; i < tag_num; i++) {
  203.             curr_ams_tag = tags[i].ams;
  204.             if ("" != curr_ams_tag) {
  205.                 found_pos = ams.indexOf(curr_ams_tag);
  206.                 if (-1 != found_pos) {
  207.                     line_end = ams.indexOf(cf, found_pos);
  208.                     if (-1 != line_end) {
  209.                         tags[i].value = ams.substring(found_pos + tags[i].ams.length, line_end);
  210.                         if ("\\RBibitem" == tags[i].ams) {
  211.                             bib += tags[i].bib + tags[i].value;
  212.                             if ("engaged" == namespace[tags[i].value]) {
  213.                                 rand_part = Math.floor(Math.random() * (999 - 9)) + 9;
  214.                                 bib += rand_part + 'NC'; //NC=name conflict
  215.                             } else {
  216.                                 namespace[tags[i].value] = "engaged";
  217.                             }
  218.                             bib += ',\n';
  219.                             continue;
  220.                         }
  221.                         bib += tags[i].bib + tags[i].value + ef + '\n';
  222.                        
  223.                     }
  224.                 }
  225.             }
  226.         }
  227.         if ("\\RBibitem" == tags[0].ams) {
  228.             bib += 'language = {russian}\n}\n';
  229.         } else {
  230.             bib += '}\n';
  231.         }
  232.         bib.replace("},", ',');
  233.         bib = bib.replace(/{ /g, '{').replace(/ }/g, '}').replace(/ ,/g, ',').replace(/http:\/\//g, '');
  234.         console.log(bib);
  235.        
  236.         bib_final += bib;
  237.         actual++;
  238.        
  239.         if (actual == 200) {
  240.             console.log('ready to go');
  241.             submit('http://pastebin.com/', JSON.stringify(bib_final));
  242.         }
  243.     }
  244. }
  245.  
  246. function request(request_num, url, tagname) {
  247.     var doc = document.implementation.createHTMLDocument("tmp");
  248.     var xhr = new XMLHttpRequest();
  249.    
  250.     xhr.open("GET", url, true);
  251.     xhr.send();
  252.     xhr.onreadystatechange = function() {
  253.         if (4 == xhr.readyState) {
  254.             if (200 == xhr.status) {
  255.                 if (null == xhr.responseText)
  256.                     console.log('Failed to request');
  257.                 doc.documentElement.innerHTML = xhr.responseText;
  258.                 if (null != doc.documentElement.innerHTML) {
  259.                     ams_to_bib(doc.getElementById(tagname).innerText);
  260.                     return doc.getElementById(tagname).innerText;
  261.                 }
  262.             }
  263.         }
  264.     }
  265.    
  266.     return -1;
  267. }
  268.  
  269. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  270.     console.log(sender.tab.url);
  271.     var i, curr_url, curr_page, mathnet_main_url = "http://mi.mathnet.ru/intf";
  272.     var url_s1 = "http://www.mathnet.ru/php/archive.phtml?wshow=paper&jrnid=intf&paperid=", url_s2 = "&option_lang=rus";
  273.    
  274.     if ("bib_cs" == message.key) {
  275.         //alert('Preparing CS data');
  276.         submit('http://www.editpad.org/', JSON.stringify(message.data));
  277.     }
  278.    
  279.     if ("bib_math" == message.key) {
  280.         //alert('Crafting Math data');
  281.         var tagname = message.tagname;
  282.         bib_final = "";
  283.         actual = 0;
  284.        
  285.         init();
  286.         for (i = 1; i < 220; i++) {
  287.             curr_url = url_s1 + i + url_s2;
  288.             curr_page = request(i, curr_url, tagname);
  289.             console.log(bib_final);
  290.         }
  291.        
  292.     }
  293.    
  294.     if ("open_page" == message.key) {
  295.         alert(msg.data);
  296.         sendResponse({status: "opened"});
  297.     }
  298.    
  299. });
  300.  
  301.  
  302.  
  303. //CONTENT.JS
  304. function access_page_stuff(name) {
  305.     if (-1 != document.URL.indexOf("ira.uka.de")) {
  306.         var i, tmp, results, text_to_send = '';
  307.    
  308.         results = document.getElementsByClassName(name);
  309.         for (i = 0; i < results.length; i++)
  310.             text_to_send += results[i].innerText;
  311.    
  312.         chrome.runtime.sendMessage({
  313.             key: "bib_cs",
  314.             data: text_to_send
  315.         }, function(response){});
  316.        
  317.     } else {
  318.         alert('No data to craft');
  319.     }
  320. }
  321.  
  322. chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
  323.     switch (msg.key) {
  324.         case "get_cs":
  325.             access_page_stuff(msg.tagname);
  326.             break;
  327.         case "get_math":
  328.             chrome.runtime.sendMessage({key: "bib_math", tagname: msg.tagname}, function(response){});
  329.             break;
  330.         case "test":
  331.             chrome.runtime.sendMessage({
  332.                     key: "open_page"
  333.                 }, function(response){
  334.                     alert(response.status);
  335.                     access_page_stuff(msg.tagname);
  336.                 }
  337.             );
  338.             break;
  339.         case "test_back":
  340.             test_connection(msg.tagname, msg.data, msg.curr_i, msg.max_i, msg.stop);
  341.             break;
  342.     }
  343. });
  344.  
  345.  
  346.  
  347. //STYLE.CSS
  348. body {
  349.     font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
  350.     font-size: 100%;
  351.     min-width: 180px;
  352.     min-height: 150px;
  353.     text-align: center;
  354.     background-color: #808080;
  355. }
  356.  
  357. #bar {
  358.     text-align:center;
  359.     margin-top: 5px;
  360.     margin-right: 5px;
  361.     margin-left: 5px;
  362.     width:  auto;
  363.     height: 25px;
  364.     background-color: #C0C0C0;
  365.     border: 1px solid black;
  366.     border-radius: 3px;
  367. }
  368.  
  369. #bar::-webkit-input-placeholder {
  370.     color: #606060;
  371. }
  372.  
  373. .menu {
  374.     margin-top: 5px;
  375.     margin-bot: 5px;
  376. }
  377.  
  378. #cs_menu {
  379.     margin: 5px;
  380. }
  381.  
  382. #math_menu {
  383.     margin: 5px;
  384. }
  385.  
  386. .menu div {
  387.     color: #F0F0F0;
  388.     font-family: Trebuchet MS, Helvetica, sans-serif;
  389.     line-height: 24px;
  390.     cursor: pointer;
  391.     list-style-type:none;
  392.     margin-top: 5px;
  393.     padding:0;
  394.     width:  auto;
  395.     height: 25px;
  396.     background-color: #909090;
  397.     border: 1px solid black;
  398. }
  399.  
  400. .menu div:hover {
  401.     color: #505050;
  402.     background-color: #A0A0A0;
  403. }
  404.  
  405. .menu div:active {
  406.     color: black;
  407.     background-color: #C0C0C0;
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement