Skip to content

Instantly share code, notes, and snippets.

@jtubert
Created January 19, 2016 04:14
Show Gist options
  • Save jtubert/2e5718d248ae14648edc to your computer and use it in GitHub Desktop.
Save jtubert/2e5718d248ae14648edc to your computer and use it in GitHub Desktop.
Chrome extension for saving top visited sites in Google spreadsheet
var settings;
var email;
var id;
loadSavedSettings();
function loadSavedSettings() {
// buttonSettings
if (!window.localStorage.buttonSettings) {
window.localStorage.buttonSettings = JSON.stringify({ "showURLs": true, "openInActiveTab": false });
}
settings = JSON.parse(window.localStorage.buttonSettings);
settings["showURLs"] = true;
}
// Event listener
function onAClick(event) {
if(!settings.openInActiveTab){
chrome.tabs.create({ url: event.srcElement.href }, function(tab) {window.close();});
} else {
chrome.tabs.query({currentWindow: true, active: true}, function(tab) {
chrome.tabs.update(null, { url: event.srcElement.href }, function(tab) {window.close();});
});
}
return false;
}
function saveInTrix(mostVisitedURLs){
var iframe = document.createElement("iframe");
iframe.setAttribute("name", "iframe");
iframe.setAttribute("style", "display:none;");
iframe.setAttribute("type", "hidden");
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "data");
input.setAttribute("value", JSON.stringify(mostVisitedURLs));
var input2 = document.createElement("input");
input2.setAttribute("type", "hidden");
input2.setAttribute("name", "email");
input2.setAttribute("value", email);
var form = document.createElement("form");
form.setAttribute("id", "tubs");
form.setAttribute("method", "post");
form.setAttribute("target", "iframe");
form.setAttribute("action", INSERT_SCRIPT_URL_HERE);
document.body.appendChild(iframe);
document.body.appendChild(form);
form.appendChild(input);
form.appendChild(input2);
document.forms["tubs"].submit();
}
// The LI elements
function createMenu(mostVisitedURLs) {
var mnuDiv = document.getElementById('mnu');
var ul = mnuDiv.appendChild(document.createElement('ul'));
saveInTrix(mostVisitedURLs);
for (var i = 0; i < mostVisitedURLs.length; i++) {
console.log(mostVisitedURLs);
var li = ul.appendChild(document.createElement('li'));
var a = li.appendChild(document.createElement('a'));
a.href = mostVisitedURLs[i].url;
a.appendChild(document.createTextNode(mostVisitedURLs[i].title));
if(settings.showURLs){
a.appendChild(document.createElement("br"));
res = mostVisitedURLs[i].url;
res = res.substring(0, 50);
if(res != mostVisitedURLs[i].url){
res=res+'...';
}
a.appendChild(document.createElement("span")).appendChild(document.createTextNode(res));
}
a.addEventListener('click', onAClick);
}
}
chrome.identity.getProfileUserInfo(function(info){
email = info.email;
id= info.id;
chrome.topSites.get(createMenu);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment