Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MAINFEST.JSON
- {
- "manifest_version": 2,
- "name": "BibTexR",
- "version": "1.6",
- "description": "Easiest way to handle your BibTex entries. Available in early access (BibTexR 1.6)",
- "browser_action": {
- "default_icon": "images/icon_new.png",
- "default_title": "BibTexR",
- "default_popup": "popup.html"
- },
- "icons": {
- "48": "images/icon_new_48.png",
- "123": "images/icon_new_128.png"
- },
- "background": {
- "scripts": ["jquery-2.1.4.js", "background.js"],
- "persistent": false
- },
- "content_scripts": [
- {
- "matches": ["<all_urls>"],
- "js": ["jquery-2.1.4.js", "content.js"]
- }
- ],
- "permissions": [
- "tabs",
- "activeTab",
- "<all_urls>",
- "history"
- ]
- }
- //POPUP.HTML
- <!DOCTYPE html>
- <html>
- <head>
- <title>Extension's Popup</title>
- <link href="style.css" rel="stylesheet">
- <script src="popup.js"></script>
- </head>
- <body>
- <input id="bar" type="text" placeholder="I'm Feeling Lucky">
- <div id="cs_menu" class="menu">
- <div id="cs_search_button"><span>Open Entries</span></div>
- <div id="cs_get_button"><span>Craft Entries</span></div>
- </div>
- <div id="math_menu" class="menu">
- <div id="math_open_button"><span>Open MathNet</span></div>
- <div id="math_get_button"><span>Request Data</span></div>
- </div>
- </body>
- </html>
- //POPUP.JS
- document.addEventListener('DOMContentLoaded', function() {
- var i;
- document.getElementById("cs_search_button").addEventListener("click", function() {
- var cs_query = document.getElementsByTagName('input')[0].value;
- if ("" == cs_query) {
- alert('Insert query for entries, please.');
- } else {
- chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
- chrome.tabs.create({
- active: true,
- url: "http://liinwww.ira.uka.de/csbib?query=" + cs_query + "&sort=score&results=bibtex"
- }, function(tab) {});
- });
- }
- });
- document.getElementById("cs_get_button").addEventListener("click", function() {
- chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
- chrome.tabs.sendMessage(tabs[0].id, {
- key: "get_cs",
- tagname: "bibtex"
- });
- });
- });
- document.getElementById("math_open_button").addEventListener("click", function() {
- chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
- chrome.tabs.create({
- active: true,
- url: "http://www.mathnet.ru/ej.phtml?option_lang=rus"
- }, function(tab) {});
- });
- });
- document.getElementById("math_get_button").addEventListener("click", function() {
- chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
- chrome.tabs.sendMessage(tabs[0].id, {
- key: "get_math",
- tagname: "citPaperAMSBIBID"
- });
- });
- });
- });
- //BACKGROUND.JS
- function _tag(ams, bib, val) {
- return {
- "ams" : ams,
- "bib" : bib,
- "val" : val
- };
- };
- var tag_num = 25;
- var tags = new Array(tag_num);
- var namespace = new Array();
- var bib_final, actual;
- var cf = "\n", ef = "},";
- 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";
- }
- function submit(url, data) {
- chrome.tabs.create({
- active: false,
- url: url
- }, function(tab) {
- if (-1 != url.indexOf('pastebin.com')) {
- chrome.tabs.executeScript(tab.id, {
- code: 'document.getElementById("paste_code").value = ' + data,
- }, function(result) {
- if (!result || null == result) {
- alert('Failed to run "save" script.\n' + chrome.runtime.lastError.message);
- return;
- }
- console.log('Submitted:' + result[0]);
- }
- );
- }
- if (-1 != url.indexOf('editpad.org')) {
- chrome.tabs.executeScript(tab.id, {
- code: 'document.getElementById("text").value = ' + data,
- }, function(result) {
- if (!result || null == result) {
- alert('Failed to run "save" script.\n' + chrome.runtime.lastError.message);
- return;
- }
- console.log('Submitted:' + result[0]);
- chrome.tabs.executeScript(tab.id, {
- code: 'document.getElementById("submitted").click()'
- });
- }
- );
- }
- }
- );
- }
- function ams_to_bib(entry) {
- if ("undefined" !== entry) {
- var i, rand_part, carry, pos, found_pos, line_end, curr_ams_tag;
- var ams = entry, bib = "";
- console.log('[' + actual + ']');
- console.log(ams);
- ams = ams.replace(/{/g, ' ').replace(/}/g, '').replace(/~/g, ' ').replace(/--/g, '—').replace(/, /g, ' and ');
- 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);
- if ("\\RBibitem" == tags[i].ams) {
- bib += tags[i].bib + tags[i].value;
- if ("engaged" == namespace[tags[i].value]) {
- rand_part = Math.floor(Math.random() * (999 - 9)) + 9;
- bib += rand_part + 'NC'; //NC=name conflict
- } else {
- namespace[tags[i].value] = "engaged";
- }
- bib += ',\n';
- continue;
- }
- bib += tags[i].bib + tags[i].value + ef + '\n';
- }
- }
- }
- }
- if ("\\RBibitem" == tags[0].ams) {
- bib += 'language = {russian}\n}\n';
- } else {
- bib += '}\n';
- }
- bib.replace("},", ',');
- bib = bib.replace(/{ /g, '{').replace(/ }/g, '}').replace(/ ,/g, ',').replace(/http:\/\//g, '');
- console.log(bib);
- bib_final += bib;
- actual++;
- if (actual == 200) {
- console.log('ready to go');
- submit('http://pastebin.com/', JSON.stringify(bib_final));
- }
- }
- }
- function request(request_num, url, tagname) {
- var doc = document.implementation.createHTMLDocument("tmp");
- var xhr = new XMLHttpRequest();
- xhr.open("GET", url, true);
- xhr.send();
- xhr.onreadystatechange = function() {
- if (4 == xhr.readyState) {
- if (200 == xhr.status) {
- if (null == xhr.responseText)
- console.log('Failed to request');
- doc.documentElement.innerHTML = xhr.responseText;
- if (null != doc.documentElement.innerHTML) {
- ams_to_bib(doc.getElementById(tagname).innerText);
- return doc.getElementById(tagname).innerText;
- }
- }
- }
- }
- return -1;
- }
- chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
- console.log(sender.tab.url);
- var i, curr_url, curr_page, mathnet_main_url = "http://mi.mathnet.ru/intf";
- var url_s1 = "http://www.mathnet.ru/php/archive.phtml?wshow=paper&jrnid=intf&paperid=", url_s2 = "&option_lang=rus";
- if ("bib_cs" == message.key) {
- //alert('Preparing CS data');
- submit('http://www.editpad.org/', JSON.stringify(message.data));
- }
- if ("bib_math" == message.key) {
- //alert('Crafting Math data');
- var tagname = message.tagname;
- bib_final = "";
- actual = 0;
- init();
- for (i = 1; i < 220; i++) {
- curr_url = url_s1 + i + url_s2;
- curr_page = request(i, curr_url, tagname);
- console.log(bib_final);
- }
- }
- if ("open_page" == message.key) {
- alert(msg.data);
- sendResponse({status: "opened"});
- }
- });
- //CONTENT.JS
- function access_page_stuff(name) {
- if (-1 != document.URL.indexOf("ira.uka.de")) {
- var i, tmp, results, text_to_send = '';
- results = document.getElementsByClassName(name);
- for (i = 0; i < results.length; i++)
- text_to_send += results[i].innerText;
- chrome.runtime.sendMessage({
- key: "bib_cs",
- data: text_to_send
- }, function(response){});
- } else {
- alert('No data to craft');
- }
- }
- chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
- switch (msg.key) {
- case "get_cs":
- access_page_stuff(msg.tagname);
- break;
- case "get_math":
- chrome.runtime.sendMessage({key: "bib_math", tagname: msg.tagname}, function(response){});
- break;
- case "test":
- chrome.runtime.sendMessage({
- key: "open_page"
- }, function(response){
- alert(response.status);
- access_page_stuff(msg.tagname);
- }
- );
- break;
- case "test_back":
- test_connection(msg.tagname, msg.data, msg.curr_i, msg.max_i, msg.stop);
- break;
- }
- });
- //STYLE.CSS
- body {
- font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
- font-size: 100%;
- min-width: 180px;
- min-height: 150px;
- text-align: center;
- background-color: #808080;
- }
- #bar {
- text-align:center;
- margin-top: 5px;
- margin-right: 5px;
- margin-left: 5px;
- width: auto;
- height: 25px;
- background-color: #C0C0C0;
- border: 1px solid black;
- border-radius: 3px;
- }
- #bar::-webkit-input-placeholder {
- color: #606060;
- }
- .menu {
- margin-top: 5px;
- margin-bot: 5px;
- }
- #cs_menu {
- margin: 5px;
- }
- #math_menu {
- margin: 5px;
- }
- .menu div {
- color: #F0F0F0;
- font-family: Trebuchet MS, Helvetica, sans-serif;
- line-height: 24px;
- cursor: pointer;
- list-style-type:none;
- margin-top: 5px;
- padding:0;
- width: auto;
- height: 25px;
- background-color: #909090;
- border: 1px solid black;
- }
- .menu div:hover {
- color: #505050;
- background-color: #A0A0A0;
- }
- .menu div:active {
- color: black;
- background-color: #C0C0C0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement