Advertisement
waterlgndx

researchAssist.js part of Chrome Extension

Dec 11th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Research Assist
  3.  */
  4.  
  5. var manualMode = false;
  6. var isSearching = false;
  7. var search = "";
  8. var prevSearch = "";
  9. var rAssist;
  10. setBookmarkRoot();
  11.  
  12. /**
  13.  *  A function that sets the bookmark node for Research Assist (and creates it)
  14.  */
  15. function setBookmarkRoot() {
  16.     chrome.bookmarks.search({'title': 'Research Assist'}, function (tmp) {
  17.       //console.log(tmp);
  18.       if (tmp.length < 1) {
  19.         chrome.bookmarks.create({'title': 'Research Assist'},
  20.                              function(newFolder) {
  21.               //console.log("created root :" + newFolder.title);
  22.               rAssist= newFolder;
  23.             });
  24.       } else {
  25.         rAssist = tmp[0];  //node is hopefully in the first slot here...
  26.       }
  27.     });
  28. }
  29.  
  30. chrome.webRequest.onCompleted.addListener(function(e){
  31.     queryUrl = e.url.split("q=")[1] || e.url.split("t=")[1];
  32.     query = queryUrl[0].split("%20").join(" ").split("+").join(" ");
  33.     prevSearch = query;
  34.     isSearching = !manualMode;
  35.     if ( query.length == 0 )
  36.       return;
  37.     prevSearch = query;
  38. }, {urls: ["*://www.google.com/*q=*", "*://www.bing.com/*q=*", "*://www.yahoo.com/*t=*", "*://www.ask.com/*q=*", "*://search.aol.com/*q=*", "*://www.searchaol.com/*q=*", "*://*.wow.com/*q=*",
  39.            "*://www.webcrawler.com/*q=*", "*://*.infospace.com/*q=*", "*://www.dogpile.com/*q=*"]});
  40.  
  41. chrome.webRequest.onCompleted.addListener(function(e){
  42.     if (isSearching && e.type=="main_frame") {
  43.       console.log(e);
  44.       var url = e.url;
  45.       var title;
  46.       console.log("trying to add", url, "   ", e);
  47.       chrome.tabs.query({active: true, currentWindow:true}, function (rTabs) {
  48.         addResearchMark(url, rTabs[0].title || url);
  49.       });
  50.     }
  51.   }, {urls: ["*://*/*"]});
  52.  
  53. /**
  54.  * Adds a "Research Mark" which is a bookmark and an entry in popup.html.
  55.  * popup.html either needs to be dynamically populated or stored otherwise a reload event will fire losing all
  56.  * of the current marks.
  57.  */
  58. function addResearchMark(url, title) {
  59.   if (document.getElementById("searches").innerHTML.search(url)==-1){ //if it's not in the list, add it.
  60.     document.getElementById("searches").innerHTML += "<li><a href='"+ url + "'>" + title + " </a> </li> ";
  61.     console.log("[#searches] adding item for ", url, title);
  62.   }
  63.   chrome.bookmarks.search({'url' : url, 'title' : title}, function (bma) {
  64.     if (bma.length==0) {
  65.     chrome.bookmarks.create({ 'parentId' : rAssist.id, 'title': title, 'url': url}, function(bm) {
  66.       console.log("[Research Mark] adding item for ", bm.url, bm.title);
  67.       });
  68.     }
  69.   });
  70. }
  71. /*
  72. chrome.history.onVisited.addListener(function (e) {
  73.   if (isSearching) {
  74.     var url = e.url;
  75.     var title = e.title || e.url;
  76.     addResearchMark(url, title);
  77.   }
  78. });*/
  79.  
  80. function pause() {
  81.     isSearching = false;
  82. }
  83.  
  84. function activate() {
  85.     isSearching = true;
  86. }
  87.  
  88. function manual() {
  89.   if (manualMode) {
  90.     document.getElementById("manualMode").innerHTML = "Automatic Mode";
  91.     manualMode = true;
  92.   } else {
  93.     document.getElementById("manualMode").innerHTML = "Manual Mode";
  94.     manualMode = false;
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement