Guest User

Untitled

a guest
Dec 13th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Happo-YLE
  3. // @namespace   ylefi
  4. // @description Onnellisempia uutisia.
  5. // @include     http://yle.fi/*
  6. // @version     1
  7. // @grant       none
  8. // ==/UserScript==
  9.  
  10. (($) => {
  11.     let swap = function (image, attrName) {
  12.         let old = image.getAttribute(attrName);
  13.         if (old) {
  14.             image.setAttribute(attrName, old.replace(/image\/upload\/\/?([^\/]+?)\//, (match, params) => {
  15.                 return `image/upload/${params},e_saturation:9001/`
  16.             }));
  17.         }
  18.     }
  19.    
  20.     let process = (node) => {
  21.         let container = $(node)
  22.         container.find('[src*="image/upload"]').each((i, image) => {
  23.             if ($(image).attr('hapokas')) return
  24.             swap(image, 'src');
  25.             $(image).attr('hapokas', true)
  26.         })            
  27.         container.find('[content*="image/upload"], [data-src*="image/upload"]').each((i, image) => {
  28.             if ($(image).attr('hapokas')) return
  29.             swap(image, 'data-src')
  30.             swap(image, 'content')
  31.             $(image).attr('hapokas', true)
  32.         })
  33.     }
  34.    
  35.     var observer = new MutationObserver(function(mutations) {
  36.       mutations.forEach(function(mutation) {
  37.         if (!mutation.addedNodes) return
  38.        
  39.         if ($(mutation.target).is('img') && mutation.attributeName == 'data-src') {
  40.             console.log(mutation.target);
  41.             process($(mutation.target).parent())
  42.         }
  43.         if ($(mutation.target).is('img') && mutation.attributeName == 'src') {
  44.             console.log(mutation.target);
  45.             process($(mutation.target).parent())
  46.         }
  47.         mutation.addedNodes.forEach(process)
  48.       })
  49.     })
  50.  
  51.     observer.observe(document.body, {
  52.         childList: true
  53.       , subtree: true
  54.       , attributes: true
  55.       , characterData: false
  56.     })
  57.    
  58.     process($('body'))
  59.    
  60. })(window.jQuery);
Add Comment
Please, Sign In to add comment