Advertisement
survivorman

[Tampermonkey] Simple Facebook Messenger

Jan 22nd, 2017
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Simple Facebook Messenger
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.3a
  5. // @description  making new Facebook Messenger design more readable
  6. // @author       RudyErudyta
  7. // @match        https://www.facebook.com/messages/t/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. // Width of the site
  12. var messengerWidth = "1500px";
  13. // Size of the messages text
  14. var textSize = "13.5px";
  15. // Width of sent photos
  16. var picWidth = "300px";
  17. // URL of your profile picture
  18. var ownProfilePicUrl = '';
  19. // Node containing your profile picture that will be copied next to your messages
  20. var ownProfilePicTemplate = null;
  21.  
  22. // Set target node of mutation observer to body
  23. var target = document.querySelector('#facebook');
  24.  
  25. // Configure of the observer; basically any DOM change invokes the observer function
  26. var config = { childList: true, subtree: true };
  27.  
  28. // Create an observer instance
  29. var observer = new MutationObserver(function(mutations) {
  30.     // Find your profile picture URL if not found already
  31.     if (ownProfilePicUrl === '')
  32.         ownProfilePicUrl = "https://graph.facebook.com/" + document.querySelector("._s0").id.split("_")[3] + "/picture?type=square";
  33.  
  34.     // Disconnect the observer to prevent it from self-invoking and looping recursively
  35.     this.disconnect();
  36.  
  37.     // Delete the classes that make the messages look the way they look
  38.     var elems = document.querySelectorAll("._o46");
  39.     [].forEach.call(elems, function(el) {
  40.         el.classList.remove("_o46");
  41.     });
  42.  
  43.     elems = document.querySelectorAll("._3erg");
  44.     [].forEach.call(elems, function(el) {
  45.         el.classList.remove("_3erg");
  46.     });
  47.  
  48.     // Remove blue background from your messages
  49.     elems = document.querySelectorAll("._nd_");
  50.     [].forEach.call(elems, function(el) {
  51.         el.classList.remove("_nd_");
  52.     });
  53.  
  54.     // Change the font to make it readable on the new background
  55.     elems = document.querySelectorAll("._3oh-");
  56.     [].forEach.call(elems, function(el) {
  57.         el.style.fontFamily = "Helvetica";
  58.         el.style.color = "#333";
  59.         el.style.fontSize = textSize;
  60.     });
  61.  
  62.     // Make names visible
  63.     elems = document.querySelectorAll("._-ne");
  64.     [].forEach.call(elems, function(el) {
  65.         el.classList.remove("_-ne");
  66.     });
  67.  
  68.     // Remove the gray styling from names
  69.     elems = document.querySelectorAll("._ih3");
  70.     [].forEach.call(elems, function(el) {
  71.         el.classList.remove("_ih3");
  72.     });
  73.  
  74.     elems = document.querySelectorAll("._hh7");
  75.     [].forEach.call(elems, function(el) {
  76.         el.classList.remove("_hh7");
  77.     });
  78.  
  79.      elems = document.querySelectorAll("._4ldz ._1t_r");
  80.     [].forEach.call(elems, function(el) {
  81.         el.classList.add("_-ne");
  82.     });
  83.  
  84.     // Align photos
  85.     elems = document.querySelectorAll("._3zvs");
  86.     [].forEach.call(elems, function(el) {
  87.         el.classList.remove("_3zvs");
  88.     });
  89.  
  90.     // Align and format others' profile pictures
  91.     elems = document.querySelectorAll("._4ld-");
  92.     [].forEach.call(elems, function(el) {
  93.         // Get rid of circles
  94.         el.classList.remove("_4ld-");
  95.         // Positioning
  96.         el.parentElement.style.top = "2px";
  97.         toRm = el.parentElement.querySelector("._2pom");
  98.         if (toRm)
  99.             el.parentElement.removeChild(toRm);
  100.  
  101.         // Based on this node create template to paste next to your messages
  102.         if (ownProfilePicTemplate === null){
  103.             var pic = el.parentElement.parentElement.parentElement.querySelector("._1t_q");
  104.             if (pic){
  105.                 ownProfilePicTemplate = pic.cloneNode(true);
  106.                 var imgTag = ownProfilePicTemplate.querySelector(".img");
  107.                 if (imgTag)
  108.                     imgTag.src = ownProfilePicUrl;
  109.             }
  110.         }
  111.     });
  112.  
  113.     // Add profile picture next to your messages; search through all the messages once again
  114.     elems = document.querySelectorAll("._41ud ");
  115.     [].forEach.call(elems, function(el) {
  116.         picTest = el.parentElement.querySelector("._1t_q");
  117.         // If the profile picture is missing - it must be your message!
  118.         if (!picTest && ownProfilePicTemplate !== null){
  119.             var addedPic = ownProfilePicTemplate.cloneNode(true);
  120.             el.parentElement.appendChild(addedPic);
  121.         }
  122.     });
  123.  
  124.     // Fix conversation margin
  125.     elems = document.querySelectorAll("._41ud");
  126.     [].forEach.call(elems, function(el) {
  127.         el.style.marginLeft = "40px";
  128.     });
  129.  
  130.     // Change pics and links width
  131.     elems = document.querySelectorAll("._4tsk");
  132.     [].forEach.call(elems, function(el) {
  133.         el.style.minHeight = "0";
  134.         el.style.width = picWidth;
  135.     });
  136.  
  137.     elems = document.querySelectorAll("._5i_d");
  138.     [].forEach.call(elems, function(el) {
  139.         el.style.minHeight = "0";
  140.         el.style.width = picWidth;
  141.     });
  142.  
  143.     // I'm sure it had a purpose, but frankly I forgot what it was
  144.     elems = document.querySelectorAll("._2n8i");
  145.     [].forEach.call(elems, function(el) {
  146.         el.classList.remove("_2n8i");
  147.     });
  148.  
  149.     // Connect the observer again
  150.     this.observe(target, config);
  151. });
  152.  
  153.  
  154. // Connect the observer for the first time
  155. observer.observe(target, config);
  156.  
  157. window.onload = function() {
  158.     // Change font size and page width
  159.     var sheet = document.createElement('style');
  160.     sheet.innerHTML = ".__i_{font-size: "+ textSize +" ;} ._4sp8 {width: " + messengerWidth + "; clear: both; margin: auto;} ._4pmj {width: " + messengerWidth + "; clear: both; margin: auto;}";
  161.     document.body.appendChild(sheet);
  162. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement