Skip to content

Commit

Permalink
fixed undefined error on load
Browse files Browse the repository at this point in the history
  • Loading branch information
emincansumer committed Dec 3, 2014
1 parent 78f8fb0 commit 0bd1fbb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ A chrome extension that fetches yify torrent data on imdb movie pages

## How to install
- Open extensions on the main menu (More Tools > Extensions)
- Check the "Developer Mode" checkbox on the top right
- Click "Load unpacked extension..." button on the top left
- Select "yify-imdb-chrome-extension" folder and click "Ok".
- Extension is loaded now.
- Download [latest release](https://github.com/emincansumer/yify-imdb-chrome-extension/releases) of extension
- Drag and drop the extension file to Chrome's extension page

## How to use
- Open a imdb movie site and click the extension icon on the top right.
12 changes: 10 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/**
* Listen popup load event
*/
document.addEventListener('DOMContentLoaded', function () {
chrome.storage.local.get('html', function(data){
if(typeof data !== 'undefined'){
if(typeof data.html !== 'undefined'){
document.body.innerHTML = data.html;
}
});
});

/**
* Listen local storage change and show result on popup
*/
chrome.storage.onChanged.addListener(function(changes, namespace) {
document.body.innerHTML = changes['html'].newValue;
if(typeof changes['html'].newValue !== 'undefined'){
document.body.innerHTML = changes['html'].newValue;
}
});
22 changes: 20 additions & 2 deletions bg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

/**
* Class that fetches and shows yify data
*/
var yifyChecker = {

apiURL : "https://yts.re/api/listimdb.json?imdb_id=",
Expand All @@ -7,6 +9,9 @@ var yifyChecker = {
this.requestJSON(imdb_id);
},

/**
* Fetches json result for the specified movie
*/
requestJSON : function(imdb_id) {
var xhr = new XMLHttpRequest();
xhr.open("GET", this.apiURL + imdb_id, true);
Expand All @@ -19,32 +24,45 @@ var yifyChecker = {
xhr.send();
},

/**
* Shows and saves the html result
*/
showTorrents : function(data) {
var bodyText = '';
if(data.status === "fail"){
bodyText += "No torrent found :(";
chrome.browserAction.setBadgeText({text:""});
// remove badge
chrome.browserAction.setBadgeText({text:""});
} else {
var count = data.MovieCount;
// build link
for(var i = 0; i < count; i++) {
var title = data.MovieList[i].MovieTitleClean + ' - ' + data.MovieList[i].Quality + ' (' + data.MovieList[i].Size + ')';
bodyText += '<a href="'+data.MovieList[i].TorrentUrl+'" target="_blank">'+title+'</a>';
}
// set badge count
chrome.browserAction.setBadgeBackgroundColor({color:[204, 0, 0, 230]});
chrome.browserAction.setBadgeText({text:count+""});
}
// save the value into local storage to user in popup
chrome.storage.local.set({'html' : bodyText});
var views = chrome.extension.getViews({type: "popup"});
for (var i = 0; i < views.length; i++) {
views[i].document.innerHTML = '';
// write the result to popup just in case
views[i].document.innerHTML = bodyText;
}
}

};

/**
* Listen page load event and run checker
*/
chrome.webNavigation.onCompleted.addListener(function(details) {
chrome.tabs.query({active:true}, function(selectedTab){
var url = selectedTab[0].url.split('/');
// run only for imdb movie pages
if((url[2] === 'imdb.com' || url[2] === 'www.imdb.com') && url[3] === 'title' && details.url === selectedTab[0].url){
yifyChecker.run(url[4]);
}
Expand Down
1 change: 0 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"permissions": [
"https://yts.re/",
"tabs",
"activeTab",
"webNavigation",
"storage"
],
Expand Down

0 comments on commit 0bd1fbb

Please sign in to comment.