-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: hackernews.js
- Loading branch information
Showing
2 changed files
with
66 additions
and
92 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,54 @@ | ||
// check for HN comments when page is navigated to | ||
function checkPageURL(tabId, changeInfo, tab) { | ||
if (changeInfo && changeInfo.status == "loading") { | ||
// get current tab URL | ||
var currentURL = tab.url, | ||
postIndex; | ||
|
||
postIndex = posInList( currentURL, hnPostList ); | ||
|
||
// check if in list of stored tabs | ||
if ( postIndex != null ) { | ||
// show pageaction | ||
chrome.pageAction.show( tabId ); | ||
} | ||
} | ||
} | ||
|
||
function posInList( item, list ) { | ||
// loop over list | ||
for ( var i = 0; i < list.length; i++ ) { | ||
// get linkURL item from currently selected | ||
var currentURL = list[i].linkURL; | ||
|
||
// return false if equivalent | ||
if ( currentURL == item ) { | ||
return i | ||
} | ||
} | ||
return null | ||
} | ||
|
||
// Add listener for pageaction | ||
chrome.pageAction.onClicked.addListener(function(tab) { | ||
// function to nav tab, action begins below | ||
function navigateTab(commentsPage) { | ||
// create URL of comments page | ||
var commentsURL = "http://news.ycombinator.com/" + commentsPage; | ||
// navigate tab to new url | ||
chrome.tabs.update(tabId, {url: commentsURL}); | ||
} | ||
var destination, | ||
commentURL, | ||
tabId, | ||
tabName; | ||
|
||
var tabId = tab.id, // get tabId from tab object | ||
tabURL, | ||
pageIndex, | ||
commentsURL; | ||
// get tab URL | ||
tabURL = tab.url; | ||
// retrieve comment from tabList | ||
tabId = tab.id; | ||
tabName = "tab_" + tabId; | ||
commentURL = tabList[ tabName ]; | ||
|
||
// get post url index then comments URl | ||
pageIndex = posInList( tabURL, hnPostList ); | ||
commentsURL = hnPostList[pageIndex].discussionURL | ||
// create URL of comments page | ||
destination = "http://news.ycombinator.com/" + commentURL; | ||
|
||
// pass URL of function to navigate tab | ||
navigateTab( commentsURL ); | ||
// navigate tab to new url | ||
chrome.tabs.update(tabId, {url: destination}); | ||
}); | ||
|
||
// listens for messages passed when chrome storage is altered | ||
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { | ||
var tabName; | ||
|
||
// check the correct message is being recieved | ||
if ( message.scrapeArray ) { | ||
var newPostList = message.scrapeArray; | ||
clickFlag = true; | ||
mostRecentComment = message; | ||
|
||
}); | ||
|
||
for ( var i = 0; i < newPostList.length; i++ ) { | ||
// tab change listener runs URL checking function | ||
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { | ||
var isLoading, | ||
tabName; | ||
|
||
var post = newPostList[i], // current item | ||
postURL = post.linkURL, // get url | ||
postIndex; | ||
isLoading = changeInfo.status; | ||
|
||
// get position in list (or null) | ||
postIndex = posInList( postURL, hnPostList ); | ||
if ( clickFlag && isLoading ) { | ||
|
||
// add to hnPostList if not already present | ||
if( postIndex == null ) { | ||
hnPostList.push(post); | ||
} | ||
} | ||
} | ||
}); | ||
// add comment url to tab object | ||
tabName = "tab_" + tabId; | ||
tabList[ tabName ] = mostRecentComment; | ||
|
||
// show page action | ||
chrome.pageAction.show( tabId ); | ||
|
||
// tab change listener runs URL checking function | ||
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { | ||
checkPageURL(tabId, changeInfo, tab); | ||
// turn clickFlag off | ||
clickFlag = false; | ||
} | ||
} ); | ||
|
||
// create array to which all scraped pages are pushed | ||
var hnPostList = []; | ||
var tabList = Object(), | ||
mostRecentComment, | ||
clickFlag; |
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