Joeviocoe / Facebook Fix Mobile

// ==UserScript==
// @name         Facebook Fix Mobile
// @namespace
// @version      1.0.7
// @description  Fix Ads, Links and Display of Facebook.
// @include      http://m.facebook.com/*
// @include      https://m.facebook.com/*
// @grant        GM_addStyle
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @icon         https://www.google.com/s2/favicons?domain=www.facebook.com
// @copyright    2018+, Joeviocoe
// @license      MIT
// @noframes
// ==/UserScript==
// Copyright (c) 2018, Joeviocoe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
// * Neither the name of Joeviocoe nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
/*jslint browser: true*/
/*global $, jQuery, alert*/

function Remove_Ads() {
    $('div#header-notices').remove();
    $('#MStoriesTray').remove();
    var links = [];
    links = $('h3:contains("Connect With Facebook"),h3:contains("People You May Know"),header:contains("Suggested"),h3:contains("Popular Across Facebook"),span:contains("Paid"),span:contains("Sponsored")');
    if ( links.length > 0 ) {
        links.parents("div").siblings("h3").each(function(){
            var text = $(this).get(0).outerText;
            console.log("Facebook Fix:  Removing Ad from " + text);
        });
        links.parents("article").remove();
    }
}

function Fix_Links() {
    $('div.story_body_container').find('h4').css('height','22px').css('padding-top','2px').parent().closest('div').css('max-height','1200px');;
    $('div.story_body_container').find('header').next('div').css('white-space','normal');
    $('a[href*="/messages/"][data-autoid]').attr('href','https://www.facebook.com/messages/?m2w').removeAttr('data-autoid').attr("target","_blank").attr("rel","noopener noreferrer");
    var links = $('a[href*="l.php?u="],a[href*="?fbclid="],a[href*="?igshid="],[onmousedown],[data-lynx-mode],[data-lynx-uri]');
    if ( links.length > 0 ) {
        console.log("Facebook Fix:  Removing redirects on " + ( links.length ) + " URLs");
        for ( var i = 0; i < links.length; i++ ) {
            var link = links[i];
            if (link.hasAttribute("onmousedown") || link.hasAttribute("data-lynx-mode")) {
                link.removeAttribute("onmousedown");
                link.removeAttribute("data-lynx-mode");
                link.removeAttribute("data-lynx-uri");
                if (link.removeEventListener) {
                    link.removeEventListener("mousedown", link.onmousedown, false);
                } else if(link.detachEvent) {
                    link.detachEvent("onmousedown", link.onmousedown);
                }
            }
            if (link.href.indexOf("igshid=") > -1) {
                link.setAttribute("href",decodeURIComponent(link.href).split("?igshid=")[0]);
            }
            if (link.href.indexOf("fbclid=") > -1) {
                link.setAttribute("href",decodeURIComponent(link.href).split("?fbclid=")[0]);
            }
            if (link.href.indexOf("attribution_link") > -1) {
                console.log(link.href);
                link.setAttribute("href","https://www.youtube.com/watch?v=" + decodeURIComponent(decodeURIComponent(link.href)).split("?v=")[1].split("&feature=")[0]);
            }
            if (link.href.indexOf("l.php?u=") > -1) {
                link.setAttribute("href","http" + decodeURIComponent(link.href).split("http").slice(-1)[0].split('&')[0]);
            }
        }
    }
}

function Fix_TextPics() {
    console.log("Fix");
     $('div[style*="background-image:url(https://scontent"],[style*="background-color:rgba"]').each(function() {
         $(this).find('span').each(function() {
             $(this).css("cssText", "color: black; visibility: visible; font-size: 15px");
         });
         $(this).removeAttr("style");
     });
}

function run() {
    Remove_Ads();
    Fix_Links();
    Fix_TextPics();
}

setTimeout( function() {
    window.addEventListener('click', function() {
        if ( counter === 0 ) {
            setTimeout (function() { run(); counter = 0; }, 500);
        }
        counter++;
    });
    window.addEventListener('scroll', function() {
        if ( counter === 0 ) {
            setTimeout (function() { run(); counter = 0; }, 500);
        }
        counter++;
    });

    var counter = 0;
    run();
},10);