Local drupal images

Fix local drupal image issue

当前为 2015-08-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Local drupal images
  3. // @namespace http://regretless.com/
  4. // @version 0.1.1
  5. // @description Fix local drupal image issue
  6. // @author Ying Zhang
  7. // @require http://code.jquery.com/jquery-latest.js
  8. // @match http://local.parents.com:1235/*
  9. // @grant none
  10. // ==/UserScript==
  11. var didNotFindImagesToReplace = 0;
  12.  
  13. function fixImageSrc(url, replaceUrl) {
  14. var $totalImagesNeedFixing = $('img[src^="' + url + '"], img[data-src^="' + url + '"]');
  15. if($totalImagesNeedFixing.length) {
  16. $totalImagesNeedFixing.each(function() {
  17. var imageSrc = $(this).attr('src') || $(this).attr('data-src');
  18. if(imageSrc) {
  19. if(imageSrc.indexOf('/sites/parents') !== -1) {
  20. var newImageSrc = imageSrc.replace(url, replaceUrl);
  21. $(this).attr('data-src', newImageSrc).attr('src', newImageSrc).data('src', newImageSrc);
  22. console.log('%c replaced img src ' + imageSrc + ' with ' + newImageSrc + ' ', 'background: #222; color: #bada55');
  23. }
  24. }
  25. });
  26. } else {
  27. console.log('%c did not find images to replace ', 'background: #222; color: #bada55');
  28. didNotFindImagesToReplace++;
  29. }
  30. $('*').filter(function() {
  31. var backgroundImage = '';
  32. if (this.currentStyle) {
  33. backgroundImage = this.currentStyle['backgroundImage'];
  34. } else if (window.getComputedStyle) {
  35. backgroundImage = document.defaultView.getComputedStyle(this,null)
  36. .getPropertyValue('background-image');
  37. }
  38. if(backgroundImage !== 'none' && backgroundImage.indexOf('/sites/parents') !== -1 && backgroundImage.indexOf(replaceUrl) === -1) {
  39. var newBackgroundImage = backgroundImage.replace(url, replaceUrl);
  40. $(this).css('background-image', newBackgroundImage);
  41. console.log('%c replaced background-image ' + backgroundImage + ' with ' + newBackgroundImage + ' ', 'background: #222; color: #bada55');
  42. }
  43. });
  44. }
  45.  
  46.  
  47.  
  48. var intervalID = window.setInterval(function() {
  49. fixImageSrc('http://9020-jy6dj02.ad.mdp.com:1235', 'http://www.parents.com');
  50. fixImageSrc('http://local.parents.com:1235', 'http://www.parents.com');
  51.  
  52. }, 1000);