Advertisement
Ladies_Man

still working

Jul 24th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function request(url) {
  2.     var xhr = new XMLHttpRequest();
  3.    
  4.     xhr.open("GET", url, true);
  5.     xhr.onreadystatechange = function() {
  6.         if (xhr.readyState == 4) {
  7.             console.log(xhr.responseText);
  8.         }
  9.     }
  10.     xhr.send();
  11.     return JSON.parse(xhr.responseText);
  12. }
  13.  
  14. function save_result(data) {
  15.     chrome.tabs.create({
  16.             active: false, url: 'http://www.editpad.org/'
  17.         }, function(tab) {
  18.             chrome.tabs.executeScript(tab.id, {
  19.                 code: 'document.getElementById("text").value = ' + data,
  20.             }, function(result) {
  21.                 if (!result || null == result) {
  22.                     alert('Failed to run content script.\n' + chrome.runtime.lastError.message);
  23.                     return;
  24.                 }
  25.                 alert('SUBMITTED: "' + result[0] + '"');
  26.                
  27.                 chrome.tabs.executeScript(tab.id, {
  28.                     code: 'document.getElementById("submitted").click()'
  29.                 });
  30.             });
  31.         }
  32.     );
  33. }
  34.  
  35. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  36.     console.log(sender.tab.url);
  37.     if (message.key == "info") {
  38.         alert('Recideved "' + message.key + '" in background');
  39.        
  40.         var i, recieved_data = JSON.stringify(message.data);
  41.        
  42.         //alert('RECIEVED:' + recieved_data);
  43.         save_result(recieved_data);
  44.  
  45.         var url = "http://www.mathnet.ru/php/contents.phtml?jrnid=intf&wshow=aindex&year=&volume=&option_lang=rus";
  46.         var mathnet_main_page = request(url);
  47.        
  48.         chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
  49.             chrome.tabs.sendMessage(tabs[0].id, {
  50.                 key: "mathnet",
  51.                 data: mathnet_main_page,
  52.                 tagname: "SLink"
  53.             }, function(response) {});  
  54.         });
  55.     }
  56.     if (message.key == "rq") {
  57.        
  58.     }
  59. });
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. var text_to_send;
  67.  
  68. function access_page_stuff(name) {
  69.     var i, tmp, results;
  70.    
  71.     results = document.getElementsByClassName(name);
  72.     alert('num of "' + name + '":' + results.length);
  73.    
  74.     text_to_send = '';
  75.     for (i = 0; i < results.length; i++) {
  76.         text_to_send += results[i].innerText;
  77.     }
  78.     //alert('SENT(' + results.length + '): ' + text_to_send);
  79.    
  80.     chrome.runtime.sendMessage({key: "info", data: text_to_send}, function(response){});
  81. }
  82.  
  83. function test_connection(tagname, data, curr_i, max_i, stop) {
  84.     if (max_i == curr_i) {
  85.         alert('STOP:' + curr_i);
  86.         return;
  87.     }
  88.    
  89.    
  90.     curr_i++;
  91.    
  92.     chrome.runtime.sendMessage({
  93.        
  94.     }, function(response) {});
  95. }
  96.  
  97. chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
  98.     switch (msg.key) {
  99.         case "start":
  100.             access_page_stuff(msg.tagname);
  101.             break;
  102.         case "mathnet":
  103.             alert(msg.data);
  104.             break;
  105.         case "test_back":
  106.             test_connection(msg.tagname, msg.data, msg.curr_i, msg.max_i, msg.stop);
  107.             break;
  108.     }
  109. });
  110.  
  111. /*
  112. chrome.runtime.onMessage.addListener( function(msg, sender, sendResponse) {
  113.     switch (msg.key) {
  114.         case "start":
  115.             alert('recieved msg 2');
  116.             get_stuff_from_page(msg.text);
  117.     }
  118. });*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement