Created
July 30, 2014 21:22
-
-
Save 1995eaton/04cffaf836873ec90559 to your computer and use it in GitHub Desktop.
A simple Chrome extension that opens random links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A background script is needed in order to store | |
// persistent objects in memory. Content scripts | |
// run a seperate instance for each open tab in Chrome. | |
var nextLink = (function(linkArray) { | |
// Initialize a copy of the original links | |
var currentSet = linkArray.slice(); | |
return function() { | |
var currentLength = currentSet.length; | |
// When all of the currentSet links have been used, | |
// reinitialize the currentSet array with the initial link array | |
if (currentLength === 0) { | |
currentSet = linkArray.slice(); | |
} | |
// Pop a random link from the currentSet array | |
return currentSet.splice(~~(Math.random() * currentSet.length), 1)[0]; | |
}; | |
})([ | |
'https://github.com', | |
'http://www.reddit.com', | |
'http://stackoverflow.com', | |
'https://developer.mozilla.org/en-US' | |
]); | |
// Call the nextLink function when the popup icon is clicked | |
chrome.browserAction.onClicked.addListener(function() { | |
chrome.tabs.create({ | |
url: nextLink() | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"name": "Random Links", | |
"version": "1.0.0", | |
"description": "", | |
"browser_action": { | |
"default_title": "Random Links" | |
}, | |
"author": "Jake Eaton", | |
"permissions": ["tabs"], | |
"background": { | |
"persistant": false, | |
"scripts": [ | |
"background.js" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment