Local drupal images

Fix local drupal image issue

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Local drupal images
// @namespace    http://regretless.com/
// @version      0.1.1
// @description  Fix local drupal image issue
// @author       Ying Zhang
// @require      http://code.jquery.com/jquery-latest.js
// @match        http://local.parents.com:1235/*
// @grant        none
// ==/UserScript==
var didNotFindImagesToReplace = 0;

function fixImageSrc(url, replaceUrl) {
    var $totalImagesNeedFixing = $('img[src^="' + url + '"], img[data-src^="' + url + '"]');
    if($totalImagesNeedFixing.length) {
        $totalImagesNeedFixing.each(function() {
            var imageSrc = $(this).attr('src') || $(this).attr('data-src');
            if(imageSrc) {
                if(imageSrc.indexOf('/sites/parents') !== -1) {
                    var newImageSrc = imageSrc.replace(url, replaceUrl);
                    $(this).attr('data-src', newImageSrc).attr('src', newImageSrc).data('src', newImageSrc);
                  
                    console.log('%c replaced img src ' + imageSrc + ' with ' + newImageSrc + ' ', 'background: #222; color: #bada55');
                }
            }
        });
    } else {
        console.log('%c did not find images to replace ', 'background: #222; color: #bada55');
        didNotFindImagesToReplace++;
    }
    $('*').filter(function() {
        var backgroundImage = '';
        if (this.currentStyle) {
            backgroundImage = this.currentStyle['backgroundImage'];
        } else if (window.getComputedStyle) {
            backgroundImage = document.defaultView.getComputedStyle(this,null)
            .getPropertyValue('background-image');
        }
        if(backgroundImage !== 'none' && backgroundImage.indexOf('/sites/parents') !== -1 && backgroundImage.indexOf(replaceUrl) === -1) {
            var newBackgroundImage = backgroundImage.replace(url, replaceUrl);
            $(this).css('background-image', newBackgroundImage);
            console.log('%c replaced background-image ' + backgroundImage + ' with ' + newBackgroundImage + ' ', 'background: #222; color: #bada55');
        }
    });
}



var intervalID = window.setInterval(function() {
    
    
    fixImageSrc('http://9020-jy6dj02.ad.mdp.com:1235', 'http://www.parents.com');
    fixImageSrc('http://local.parents.com:1235', 'http://www.parents.com');
    

}, 1000);