2013-03-30 122 views
0

我編寫了以下Greasemonkey腳本,以便在網站http://www.pib.nic.in/newsite/erelease.aspx?relid=0中輕鬆實現閱讀體驗。xmlhttp發佈請求後觸發Greasemonkey

該代碼工作得很好。但只有一個問題。在選擇左側的下拉框來查看舊檔案時,我無法通過xmlhttp請求調用正在發生的函數contentreplace()。我試圖通過匿名函數在最後使用時間延遲來實現它。由於每次加載時間都不一樣,函數的調用不會始終發生。

如果在xmlhttp post請求之後有一種方法調用函數contentreplace(),這對於使代碼更完美將會非常有幫助。

// ==UserScript== 
// @name   Press Information Bureau Main 
// @description  PIB Open links in new tab, Color change on click, make scrollable 
// @include   http://pib.nic.in* 
// @include   http://www.pib.nic.in* 
// @require   http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js 
// @grant   GM_addStyle 
// @version   3 
// ==/UserScript== 
/*- The @grant directive is needed to work around a design change 
introduced in GM 1.0. It restores the sandbox. 
*/ 




if(window.location.toString() =='http://www.pib.nic.in/' ) { 
    //do nothing 
} 
else if(window.location.toString() =='http://www.pib.nic.in/newsite/mainpage.aspx' )  { 
    //do nothing 
} 

else if($.trim(window.location.toString())=='http://www.pib.nic.in/newsite/erelease.aspx?relid=0') { 


contentreplace(); 
makescroll(); 
lichangecolor(); 
} 
else { 

deletenodes(); 
} 


//------------ function contentreplace() ---------------// 

//Function to replace the Getrelease to the url 

function contentreplace(){ 

//-- Use jQuery selector to get just the <li>s that have an onclick. 
var articleLinks = $("#lreleaseID li[onclick]"); 

//-- jQuery .each() 
articleLinks.each (function() { 
var jThis   = $(this); // "this" is a special var inside .each() 
var onClickVal  = jThis.attr ("onclick"); 
var articleMatch = onClickVal.match (/Getrelease\s*\((\d+)\)/i); 

if (articleMatch && articleMatch.length == 2) { 
    var articleId = articleMatch[1]; 
    var articleUrl = "http://www.pib.nic.in/newsite/erelease.aspx?relid=" 
        + articleId 
        ; 

    jThis.attr ("onclick", "window.open('" + articleUrl + "')"); 

} 
}); 
} 

//------------- function makescroll() -------------------------------// 

//Function to change the left side scorllable 
function makescroll(){ 
$('.leftrightdiv').css('height','300px'); 
$('.leftrightdiv').css('overflow','scroll'); 
} 


//--------------- function lichangecolor() ---------------------------------------// 


//li item change color on click 
function lichangecolor(){ 
$("li").click(function(){ 

$(this).css("color","red"); 
}); 
} 


//-------------------- function deletenodes() --------------------------// 
//Function to remove nodes 
function deletenodes(){ 

//delete this for ministry delete 
ministry=document.getElementById('minID').parentNode; 
ministry.parentNode.removeChild(ministry); 

//delete this for newslinks delete 
newslins=document.getElementById('relhead').parentNode; 
newslins.parentNode.removeChild(newslins); 

//delete this for newslinks near by delete 
    newslinksadj=document.getElementById('relhead').parentNode.parentNode.getElementsByTagName('td')[9]; 
newslinksadj.parentNode.removeChild(newslinksadj); 

} 




//---------------- function $() -----------------------// 

//Function for detecting drop down change 
(function($) { 
$.fn.selected = function(fn) { 
    return this.each(function() { 
     var clicknum = 0; 
     $(this).click(function() { 
      clicknum++; 
      if (clicknum == 2) { 
       clicknum = 0; 
       fn(this); 
      } 
     }); 
    }); 
} 
})(jQuery); 


//------------ Anonymous functions() ---------// 


//On change of Ministry 

$('#minID').selected(function() { 

setTimeout(
    function() 
    { 
    contentreplace(); 
    lichangecolor(); 
    }, 900); 

}); 

//On change of Date Drop down 
$('#rdateID').selected(function() { 
setTimeout(
    function() 
    { 
    contentreplace(); 
    lichangecolor(); 
    }, 900); 

}); 

//On change of Month Drop down 
$('#rmonthID').selected(function() { 
setTimeout(
    function() 
    { 
contentreplace(); 
lichangecolor(); 
    }, 900); 

}); 

//On change of year Drop down 
$('#ryearID').selected(function() { 
setTimeout(
    function() 
    { 
    contentreplace(); 
    lichangecolor(); 
    }, 900); 

}); 

回答

-1

很難找到,然後從頁面腳本中修改XMLHttpRequest實例。

但還有其他方法,聽取dom的更改,然後重新運行您的替換功能。您可以使用MutationObserver

這裏是代碼片段聽存檔列表中的變化

var contentreplace = function(mutation) { 
    alert('changed'); 
}; 

var target = document.querySelector('#lreleaseID'); 
var observer = new MutationObserver(function(mutations) { 
    mutations.forEach(contentreplace); 
}); 
observer.observe(target, {childList: true});