0

I am trying to create a chrome extension that will save your google search inputs in a CSV file that is downloadable. For example: I enter the search input in google search "programming python tutorials". When search input is typed, I should be able to click the action button chrome extension to save the search input in an organized CSV file that is downloadable. This is similar to web scraping, but not really. I don't want the website information saved, I just want the search inputs saved. I have searched the chrome extension web store and online tutorials, but really haven't found anything on this. Can you please suggest where I can get started? Thank you.

2 Answers2

0

What you want is a content script that is run only on Google search. eg

{
  "name": "Log Google",
  "version": "1.0",
  "description": "Log Google search data",
  "permissions": ["tabs", "*://*.google.com/*"],
  "content_scripts": [
    {
      "matches": ["*://*.google.com/*"],
      "js": ["logdata.js"]
    }
  ]
}

Where logdata.js is the code that you want to run whenever you visit the search page. I would suggest parsing the url of the page and getting the query rather than sorting through html elements.

To get the url of the current tab:

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
    var url = tabs[0].url;
});

As for writing to the disk, take a look at the chrome.fileSystem api.

Hope this helps.

IeuanG
  • 504
  • 5
  • 13
  • This seems like a great start. Looking through the chrome.fileSystem api, I was unable to find a method that saves the search inputs as a CSV file. Is there a specific method that saves the search inputs in CSV file format? – user3692559 Feb 21 '16 at 00:10
  • No, the fileSystem api does not save items in a specific way, it's just a way to write files to disk. You could use [this method](http://stackoverflow.com/questions/201724/easy-way-to-turn-javascript-array-into-comma-separated-list) to generate a csv file from an array. – IeuanG Feb 21 '16 at 00:14
  • So i've tried a few things. However I am still not obtaining any results. If we forget about the CSV format, how can we log the search inputs or continuously record search inputs by clicking the action icon extension button? – user3692559 Mar 13 '16 at 18:48
  • What I have so far are files: icon.png; logdata.js; manifest.json; popup.html – user3692559 Mar 13 '16 at 18:49
  • logdata.js: 'document.addEventListener('DOMContentLoaded', function() { var checkPageButton = document.getElementById('checkPage'); checkPageButton.addEventListener('click', function() { chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) { var url = tabs[0].url; }, false); }, false);' – user3692559 Mar 13 '16 at 18:50
  • manifest.json: { "manifest_version": 2, "name": "Log Google", "version": "1.0", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "description": "Log Google search data", "permissions": ["tabs", "*://*.google.com/*"], "content_scripts": [ { "matches": ["*://*.google.com/*"], "js": ["logdata.js"] } ] } – user3692559 Mar 13 '16 at 18:53
  • popup.html: Open Log

    Logger

    – user3692559 Mar 13 '16 at 18:54
  • Sorry, i'm really new at this. If there is a way to input my code correctly, please let me know. – user3692559 Mar 13 '16 at 18:54
0
document.addEventListener('DOMContentLoaded', function() {
var checkPageButton = document.getElementById('checkPage');
checkPageButton.addEventListener('click', function() {

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var url = tabs[0].url;

}, false);
}, false);

//logdata.js

{
  "manifest_version": 2,
  "name": "Log Google",
  "version": "1.0",

  "browser_action": {
  "default_icon": "icon.png",
  "default_popup": "popup.html"
  },

  "description": "Log Google search data",
  "permissions": ["tabs", "*://*.google.com/*"],
  "content_scripts": [
  {
  "matches": ["*://*.google.com/*"],
  "js": ["logdata.js"]
  }
  ]
  }

//manifest.json

<!doctype html>
<html>
<head>
<title>Open Log</title>
<script src="logdata.js"></script>
</head>
<body>
<h1>Logger</h1>
<button id="checkPage">Results</button>
</body>
</html>

popup.html