simg / LinkedIn Enhancer

// ==UserScript==
// @name         LinkedIn Enhancer
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Enhance your LinkedIn Experience!
// @author       Simon Gardner
// @match        https://www.linkedin.com/feed*
// @license      MIT
// @copyright    Simon Gardner (http://holisticsystems.co.uk/)
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// @require https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/simplemodal/jquery.simplemodal.1.4.4.min.js
// ==/UserScript==


(function() {
    'use strict';

    var settings = parseSettings();
    var interval = 0;

    $(document).ready(function(){
      addStyles();

      setInterval(addMenuItem, 200); //foil naughty linkedIn devs trying to stop modifications to the DOM?
      interval = 200; processFeed();
    });


    $(window).scroll(function(){
      //wait a short while after scrolling for linkedin to de-occlude then process
      if (interval > 0) return;
      interval = 200;
      setTimeout(processFeed, interval);
    });

    function processFeed() {
        console.log("filtering feed", interval);
        if (interval == 0) return;

        if ($("#voyager-feed .core-rail div.relative.ember-view article").length == 0) {
           //LinkedIn isn't ready yet so try again shortly
           interval *= 1.5;
           setTimeout(processFeed, interval);
           return;
        }

        $("#voyager-feed .core-rail div.relative.ember-view article:not(.enhanced)").each(function(i, ele){
            processElement(ele, settings);
        });
        interval = 0;
    }

    function processElement(ele, settings) {

        $(ele).addClass("enhanced");
        if (matchFilters($(ele).text(), settings)) {

            $(ele).addClass("feed-update-collapsed");
            var btn = $("<button class=\"collapse-button\">filtered</button>")
                .on("click", function(event) {
                    $(ele).toggleClass("feed-update-collapsed");
                });
            $(ele).find(".feed-shared-top-bar .tap-target").before(btn);

        } else {
           console.log("not matched");  
        }
        /*if (settings.preventVideoAutoplay === true) {
          console.log("trying to prevent autoplay");
          $(".video-embedded .video-player .embedly", ele).remove();
        }*/
    }

    function matchFilters(str, settings) {
      for (var i=0; i < settings.feedText.length; i++) {
        var filter = settings.feedText[i];
        if (str.match(filter[0])) {
          if (filter[1]) {
            var m = Math.random();
            if (m >= filter[1]) {
              return true; //only match sometimes, allowing "volume adjustment"
            }
          } else {
            return true; //no volume set, so always return true if matched
          }
        }
      }
      return false;
    }

    function parseSettings() {
      var settings = {};
      settings.feedText = (localStorage.getItem("linkedInEnhancer-filter-settings") || "").split("\n");
      settings.feedText = settings.feedText.map(function(f) {
         var t = f.split("=");
         if (t[1]) t[1] = parseFloat(t[1]);
         return t;
      }).filter(function(f){
         return (f[0].length > 0);
      });

      if (localStorage.getItem("linkedInEnhancer-prevent-autoplay") == "true"){
        settings.preventVideoAutoplay = true;
      } else {
        settings.preventVideoAutoplay = false;
      }

      return settings;
    }

    function addMenuItem() {

      if ($("#linkedInEnhancerMenuLink").length > 0) return;//already added, so don't do again //TODO: don't keep checking this for every single DOM mutation

      $("#profile-nav-item .nav-settings__dropdown-options--manage ul")
        .append("<li id=\"linkedInEnhancerMenuLink\" class=\"nav-dropdown__item\"><a href=\"javascript:return false;\" class=\"block pv1 ph5 ember-view\">LinkedIn Enhancer</a></li>")
        .on("click", "#linkedInEnhancerMenuLink", function(event) {
           $.modal(filterSettingsHtml(),{
               minHeight:400,
               minWidth: 600,
               overlayClose:true,
               focus:false,
               onShow:function(dialog){
                 $("#LinkedInEnhancer-filter-settings").val(localStorage.getItem("linkedInEnhancer-filter-settings"));
                 if (localStorage.getItem("linkedInEnhancer-prevent-autoplay") == "true") {
                   $("#LinkedInEnhancer-prevent-autoplay").attr("checked", true);
                 }
                 $(".ok", dialog.data).click(function(){
                    $.modal.close();
                 });
               },
               onClose:function(dialog){
                 localStorage.setItem("linkedInEnhancer-filter-settings", $("#LinkedInEnhancer-filter-settings").val() );
                 if ($("#LinkedInEnhancer-prevent-autoplay").is(":checked")) {
                   localStorage.setItem("linkedInEnhancer-prevent-autoplay", true);
                 } else {
                   localStorage.setItem("linkedInEnhancer-prevent-autoplay", false);
                 }
                 $.modal.close(); // seems to be a bug because dialog doesn't actually close until second click
               }
           });
        });
    }

    function filterSettingsHtml() {
       return [
           "<div class=\"LinkedInEnhancerSettings\">",
           "<h1>LinkedIn Enhancer v.0.2</h1>",
           "<div><label for=\"LinkedInEnhancer-filter-settings\">News Feed Filters</label>",
           "<textarea rows=\"10\" class=\"mentions-input\" id=\"LinkedInEnhancer-filter-settings\" id=\"LinkedInEnhancer-filter-settings\"></textarea></div>",
           //"<div class=\"inline\"><label for=\"LinkInEnhancer-prevent-autoplay\">Prevent Video Autoplay</label> <input type=\"checkbox\" id=\"LinkedInEnhancer-prevent-autoplay\" /></div>",
           "<div class=\"actions\"><a href=\"https://openuserjs.org/scripts/simg/LinkedIn_Enhancer\" target=\"_blank\">Help</a> &nbsp; <button class=\"ok\">OK</button></div>",
           "</div>"
         ].join("");
    }

    function addStyles() {
      var styles = [
          "<style>",
          ".feed-update-collapsed section,",
          ".feed-update-collapsed .feed-shared-update__social-info,",
          ".feed-update-collapsed .feed-shared-update__comments,",
          ".feed-update-collapsed .feed-shared-update__social-actions,",
          ".feed-update-collapsed .feed-shared-update__comments-container,",
          ".feed-update-collapsed .feed-shared-hero-entity,",
          /*".feed-update-collapsed .image,",
          ".feed-update-collapsed .text-entity,",
          ".feed-update-collapsed .shared-image,",
          ".feed-update-collapsed .content .content,",
          ".feed-update-collapsed ul.actions,",
          ".feed-update-collapsed .content .social-summary,",
          ".feed-update-collapsed .content .comment-box,",
          ".feed-update-collapsed .content .comment-list,",
          ".feed-update-collapsed .side-article,",
          ".feed-update-collapsed .show-previous-comments,",
          ".feed-update-collapsed ul.rollup,",
          ".feed-update-collapsed .video,",
          ".feed-update-collapsed .recommendations,",
          ".feed-update-collapsed .group-activity,",
          ".feed-update-collapsed .show-more-updates,",
          ".feed-update-collapsed .see-more-container,",*/
          ".feed-update-collapsed .header .meta {display:none !important;}",
          ".collapse-button {background:none; border:none; color:orange; font-size:10px; font-weight:bold; cursor:pointer; margin-right:15px; display:inline-block;}",
          //".feed-update .collapse-button:after {content:\"\\e027\"; font-family:\"LinkedIn-Glyphs-2.0.7\",\"LinkedIn-Glyphs\"; color:#999;}",
          //".feed-update-collapsed .collapse-button:after {content:\"\\e026\";}",
          "#linkedInEnhancerMenuLink {cursor:pointer}",
          "#simplemodal-overlay {background-color:#000;}",
          "#simplemodal-container {background-color:#fff; border:8px solid #444; padding:12px;}",
          ".LinkedInEnhancerSettings {position:relative;}",
          ".LinkedInEnhancerSettings .help {position:absolute; bottom:10px; left:10px;}",
          ".LinkedInEnhancerSettings h1 {margin-bottom:1em;}",
          ".LinkedInEnhancerSettings label {font-size:80%; display:block; color:#666; margin-bottom:2px;}",
          ".LinkedInEnhancerSettings .inline {margin-top:10px;}",
          ".LinkedInEnhancerSettings .inline label {display:inline; color:#000;}",
          ".LinkedInEnhancerSettings .inline input {vertical-align:bottom;}",
          ".LinkedInEnhancerSettings textarea {width:95%; min-height:100px; font-size:85%; padding:0.5em; line-height:125%;}",
          ".LinkedInEnhancerSettings .actions {text-align:right; margin:1em 0.5em;}",
          ".LinkedInEnhancerSettings button {border-radius:2px;}",
          "#LinkedInEnhancer-prevent-autoplay {opacity:1; margin:0; position:static;}",
          "</style>"];
      $("head").append(styles.join(""));
    }
 })();