Last active
May 7, 2019 07:43
-
-
Save Ragnarokkr/81f215c5f860f7c144f84198f844e81f to your computer and use it in GitHub Desktop.
Facebook Mobilizer User Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Facebook Mobilizer | |
| // @namespace https://marcotrulla.it/ | |
| // @description Add a link to the mobile version of any post in timeline | |
| // @author Marco Trulla | |
| // @match *://www.facebook.com/* | |
| // @version 0.0.3 | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| // Select FB's content node (timeline) that will be observed for mutations | |
| let targetNode = document.getElementById('content'); | |
| // We observe only modifications in childList and subtree | |
| let config = { attributes: false, childList: true, subtree: true }; | |
| // On changes we add the links | |
| let callback = function(mutationsList, observer) { | |
| for(let mutation of mutationsList) { | |
| if (mutation.type == 'childList') { | |
| addMobileLinks(); | |
| } | |
| } | |
| }; | |
| // Start observing our contents | |
| let observer = new MutationObserver(callback); | |
| observer.observe(targetNode, config); | |
| // This callback function get all stories not yet processed and add the link to the mobile version | |
| function addMobileLinks() { | |
| // Retrieves the subtitles not yet mobilized | |
| let postSubtitle = document.querySelectorAll('[data-testid=story-subtitle]:not([data-mobilized=true]'); | |
| for (let post of postSubtitle) { | |
| // Retrieve the story detail link | |
| let postDetailLink = post.querySelector(':nth-child(3)').querySelector('a'); | |
| // Clone the link, set the mobile URL, and append it to the subtitle | |
| if (postDetailLink) { | |
| let mobileDetailLink = postDetailLink.href.replace('www.facebook.com', 'm.facebook.com'); | |
| let mobileLink = postDetailLink.cloneNode(); | |
| mobileLink.href = mobileDetailLink; | |
| mobileLink.textContent = 'Mobile '; | |
| post.insertBefore(mobileLink, post.lastChild); | |
| // Set the subtitle as processed | |
| post.dataset.mobilized = true; | |
| } | |
| } | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment