Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Conflicts:
	hackernews.js
  • Loading branch information
tonyonodi committed Jul 13, 2014
2 parents d294d22 + 8dcffaa commit 5deac5d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 92 deletions.
104 changes: 35 additions & 69 deletions background.js
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;
54 changes: 31 additions & 23 deletions hackernews.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,26 @@ function getCommentURL( linkToPost ) {
// check if next row was found
if( commentRow ) {
commentLink = commentRow.querySelector("a:last-child"); // get the last link in that row
if ( commentLink )

// comments don't exist for "x is hiring" listings
if ( commentLink )
commentURL = commentLink.getAttribute("href"); // get the href attribute of comment link

// return comment url
return commentURL;
}
}

// convert nodeList to array and get rid
function nodelistToArray( nodelist ) {
var array = [],
arrayAsObj;
var array = [];

for (var i = 0; i < nodelist.length; i++ ) {
var postObject, // to be filled with post object later
currentPost = nodelist[i], // the node for the current post
postURL = currentPost.getAttribute("href"), // URL for current post link
discussionURL = getCommentURL(currentPost); // pass to function; get comment URL
var currentPost = nodelist[i]; // the node for the current post

// Create post object
postObject = {
"linkURL": postURL,
"discussionURL": discussionURL
}

// add if it's not a "more" link
if( postObject.discussionURL ) array.push( postObject );
}

// create object for array
arrayAsObj = {
"scrapeArray": array
// Add node to array
array.push( currentPost );
}

// send array to background
chrome.runtime.sendMessage( arrayAsObj );

return array;
}
Expand All @@ -58,3 +43,26 @@ var linkList,
// grab all links including "more" link and convert to array of objects
linkList = document.querySelectorAll( "td.title a" );
linkArray = nodelistToArray( linkList );

function messenger( message ) {
// send comment url
chrome.runtime.sendMessage( message );
}

// use < to omit "more"
for (var i = 0; i < linkArray.length; i++) {
var link,
comment;

link = linkArray[i];
comment = getCommentURL( link );

if ( comment ) {

link.addEventListener( "click", messenger.bind( null, comment ), false);

}
};



0 comments on commit 5deac5d

Please sign in to comment.