Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Functioning version!

  • Loading branch information...
commit 64183ab146393bd9804cfdb658d72ea1a3273c50 1 parent 219d4ee
@fzakaria authored
View
BIN  Images/headphones_blue.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  Images/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
32 background.html
@@ -0,0 +1,32 @@
+
+ <script>
+ //Global accessor that the popup uses
+ var songs = [];
+ //this is done so we can have our page action on every beatport page!
+ function updatePageAction(tabId)
+ {
+ chrome.tabs.sendRequest(tabId, {is_beatport: true}, function(response) {
+ if (response.is_beatport)
+ chrome.pageAction.show(tabId);
+ songs = [];
+ });
+ };
+
+ chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
+ if (change.status == "complete") {
+ updatePageAction(tabId);
+ }
+ });
+
+
+ function content_script_finished(request, sender, response)
+ {
+ //Grab the request and fill in the songs map for the popup.html
+ songs.push(request);
+ //Return nothing to let the connection be cleaned up
+ response({});
+ };
+
+ chrome.extension.onRequest.addListener(content_script_finished);
+
+ </script>
View
48 content_script.js
@@ -6,6 +6,17 @@ var SCRAPE_DEBUG = false;
var timeout = null;
var API_MAP = { "track" : "http://api.beatport.com/catalog/tracks", "release" : "http://api.beatport.com/catalog/tracks",
"iTunes" : "http://itunes.apple.com/search" };
+
+
+// The background page is asking us to find an address on the page.
+if (window == top) {
+ chrome.extension.onRequest.addListener(function(req, sender, sendResponse) {
+ if (req.is_beatport)
+ start();
+ sendResponse({is_beatport: true});
+ });
+};
+
/*
Debug specific functions go here
*/
@@ -17,7 +28,6 @@ function dbprint(value)
console.debug(value);
}
}
-
/*
In order to continously scrape the songs as the user browses around,
this hack works by listening to the DOMTree being modified. We need to attach
@@ -26,7 +36,6 @@ a timeout however because it fires multiple times for a single page change
function listener()
{
dbprint("listener fired.");
- start();
}
document.addEventListener("DOMSubtreeModified", function() {
@@ -69,6 +78,11 @@ function assign_song_values(beatport_song, track)
beatport_song.title = track.name;
beatport_song.price = track.price.usd; //let's just do USD for now
beatport_song.qualified_name = track.name + " ("+ track.mixName + ")";
+ beatport_song.artists = [];
+ for (var artistIndex in track.artists)
+ {
+ beatport_song.artists.push( track.artists[artistIndex].name);
+ }
};
function lookup_cheaper_songs(bp_song)
@@ -81,12 +95,32 @@ function search_itunes(bp_song)
$.getJSON(API_MAP["iTunes"],{ term : bp_song.qualified_name.replace(/\s/g,'+')}, function(data){
if (data.resultCount < 1)
return;
- console.debug("searching: "+bp_song.qualified_name);
for (var trackIndex in data.results)
{
var track = data.results[trackIndex];
- console.debug("\tFound track: " + track.trackName);
- console.debug("\tFor the price: "+ track.trackPrice);
+ var isMatch = false;
+ for (var artistIndex in bp_song.artists)
+ {
+ var pattern= new RegExp(bp_song.artists[artistIndex], "g");
+ if (pattern.test(track.artistName))
+ {
+ //We've found a pretty strong match!
+ if (track.trackPrice < bp_song.price && track.trackPrice > 0) //trackPrice of -1 is AlbumOnly
+ {
+ bp_song.price = track.trackPrice;
+ bp_song.url = track.trackViewUrl;
+ console.debug("Found cheaper track!");
+ console.debug("\tTitle: "+bp_song.qualified_name);
+ console.debug("\tPrice: "+bp_song.price);
+ console.debug("\tUrl: "+ bp_song.url);
+ //send the scraped songs to the background.html file to be displayed in the popup
+ chrome.extension.sendRequest(bp_song, function(response) {
+ //we don't care bout the response
+ });
+ }
+ }
+ }
+
}
});
};
@@ -94,10 +128,11 @@ function search_itunes(bp_song)
BeatportSong.prototype.type = "SongType";
BeatportSong.prototype.id = "SongID";
-BeatportSong.prototype.artist = "SongArtist";
+BeatportSong.prototype.artists = [];
BeatportSong.prototype.title = "SongTitle";
BeatportSong.prototype.qualified_name = "Name&Mix";
BeatportSong.prototype.price = 0;
+BeatportSong.prototype.url = "";
/*
@@ -137,7 +172,6 @@ function start()
newSong = new BeatportSong(item_type, item_id);
scraped_songs.push(newSong);
-
});
};
View
BIN  icon.png
Deleted file not rendered
View
10 manifest.json
@@ -1,14 +1,22 @@
{
- "name": "Cheap Song Finder",
+ "name": "Beatport Song Matcher",
"version": "1.0",
"description": "Tries to find cheapest song while browsing beatport.",
+ "background_page": "background.html",
+ "page_action": {
+ "default_name" : "Find cheap songs",
+ "default_icon": "Images/icon.png",
+ "default_popup": "popup.html"
+ },
"content_scripts":[
{
"matches":["http://www.beatport.com/*"],
+ "run_at" : "document_idle",
"js":["jquery.js", "content_script.js"]
}
],
"permissions": [
+ "tabs",
"http://api.beatport.com/*",
"http://www.beatport.com/*",
"http://*/*"
View
76 popup.html
@@ -0,0 +1,76 @@
+<html>
+<head>
+<style type="text/css">
+body {
+ font: 12px 'Myriad Pro', 'Tw Cen MT', Arial, Verdana, sans-serif;
+ color: #666666;
+ min-width:300px;
+ float: left;
+}
+ul {
+ padding: 0;
+ list-style: none;
+}
+li {
+ clear: both;
+ padding: 2px 0;
+}
+li div img {
+ margin: 0 5px;
+ vertical-align: middle;
+}
+li div {
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ overflow: hidden;
+ width: 250px;
+ float: left;
+ padding: 2px 0;
+}
+li span {
+ margin-left: 5px;
+}
+li:hover {
+ background-color: #fffccc;
+}
+a {
+ color: #4E7DC2;
+ text-decoration: none;
+}
+a:hover {
+ color: #880000;
+ text-decoration: underline;
+}
+
+table{
+ width:auto;
+ padding:0px;
+ margin:0px;
+}
+
+</style>
+<script>
+var bkg = chrome.extension.getBackgroundPage();
+var cheaper_songs = bkg.songs;
+
+
+document.write('<table width="20" border="0">');
+
+for (var index in cheaper_songs)
+{
+ var cheaper_song = cheaper_songs[index];
+ document.write('<tr>');
+
+ document.write('<td>' + cheaper_song.qualified_name + '</td>');
+ document.write('<td>' + cheaper_song.price + '</td>');
+ document.write('<td> <a href="' + cheaper_song.url + '">Link</td>');
+ document.write('</tr>');
+}
+
+document.write('</table>');
+</script>
+</head>
+<body>
+
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.