jhult / LinkedIn Hide Sponsored Updates

// ==UserScript==
//
// @name        LinkedIn Hide Sponsored Updates
// @description Hide Sponsored Updates from your LinkedIn news feed.

// @namespace   http://jonathanhult.com/
// @author      Jonathan Hult
// @license     MIT

// @version     1.0
// @changelog   1.0 Initial version

// @match       *://*.linkedin.com/
// @match       *://*.linkedin.com/?*
// @match       *://*.linkedin.com/*home*

// ==/UserScript==

	var old_scrollY = 0;
	var scroll_events = 0;
	var lastChecked = 0;
	window.addEventListener('scroll', onScroll, false);
	removeAllSponsoredPosts();
	
	function onScroll(e) {
	      var now = new Date().getTime();
	      
	      if ((now - lastChecked) > 1000) {
			var y = window.scrollY;
			// if (scroll_events === 0) old_scrollY = y; // stops only if scroll position was on 2. page
			var delta = e.deltaY || y - old_scrollY; // NOTE: e.deltaY for "wheel" event
			if (delta > 0 && (window.innerHeight + y) >= (document.body.clientHeight - window.innerHeight)) {
				  try {
					  removeAllSponsoredPosts();
				  } catch (err) {
					  console.error(err.name + ": " + err.message);
				  }
			}
			old_scrollY = y;
			scroll_events += 1;
	      }
	}

	function removeAllSponsoredPosts(){
		lastChecked = new Date().getTime();
		
		// Find all updates
		var updates = document.querySelectorAll('#my-feed-post');

		if (typeof updates != 'undefined') {

			// Loop through each update
			for (var i = 0; i < updates.length; i++) {

				// Find Pulse updates (as determined by the class linkedin-recommend-pulse)
				var pulseUpdate = updates[i].querySelector('.linkedin-recommend-pulse');
				
				if (pulseUpdate !== null) {
					pulseUpdate.parentNode.removeChild(pulseUpdate);
				}
				
				// Find sponsored updates (as determined by the class linkedin-sponsor)
				var sponsoredUpdate = updates[i].querySelector('.linkedin-sponsor');
				
				if (sponsoredUpdate !== null) {
					sponsoredUpdate.parentNode.removeChild(sponsoredUpdate);
				}
			}
		}
	}