Jquery 在xmlhttp post请求后触发Greasemonkey
我写了下面的Greasemonkey脚本,以使网站的阅读体验更轻松 代码运行得非常好。但只有一个问题。在选择左侧的下拉框以查看旧存档时,我无法通过xmlhttp请求调用函数contentreplace()。我试图通过匿名函数在最后使用延时来实现它。由于加载时间每次都不同,因此对函数的调用并不总是发生 如果有一种方法可以在xmlhttp post请求之后调用函数contentreplace(),这将对完善代码有很大帮助Jquery 在xmlhttp post请求后触发Greasemonkey,jquery,greasemonkey,Jquery,Greasemonkey,我写了下面的Greasemonkey脚本,以使网站的阅读体验更轻松 代码运行得非常好。但只有一个问题。在选择左侧的下拉框以查看旧存档时,我无法通过xmlhttp请求调用函数contentreplace()。我试图通过匿名函数在最后使用延时来实现它。由于加载时间每次都不同,因此对函数的调用并不总是发生 如果有一种方法可以在xmlhttp post请求之后调用函数contentreplace(),这将对完善代码有很大帮助 // ==UserScript== // @name P
// ==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);
});
/==UserScript==
//@name新闻局主页
//@description PIB在新选项卡中打开链接,单击即可更改颜色,使其可滚动
//@包括http://pib.nic.in*
//@包括http://www.pib.nic.in*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
//@grant GM_addStyle
//@version 3
//==/UserScript==
/*-需要@grant指令来解决设计变更
在GM 1.0中引入。它会恢复沙箱。
*/
if(window.location.toString()='http://www.pib.nic.in/' ) {
//无所事事
}
else if(window.location.toString()='http://www.pib.nic.in/newsite/mainpage.aspx' ) {
//无所事事
}
如果($.trim(window.location.toString())='http://www.pib.nic.in/newsite/erelease.aspx?relid=0' ) {
contentreplace();
makescroll();
lichangecolor();
}
否则{
deletenodes();
}
//------------函数contentreplace()---------------//
//函数将Getrelease替换为url
函数contentreplace(){
//--使用jQuery选择器仅获取具有onclick的s。
var articleLinks=$(“#lreleaseID li[onclick]”);
//--jQuery.each()
articleLinks.each(函数(){
var jThis=$(this);/“this”是.each()中的一个特殊var
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="
+文章ID
;
jThis.attr(“onclick”、“window.open”(“+articleUrl+”)”);
}
} );
}
//-------------函数makescroll()-------------------------------//
//用于更改左侧可调焦距的功能
函数makescroll(){
$('.leftrightdiv').css('height','300px');
$('.leftrightdiv').css('overflow','scroll');
}
//---------------函数lichangecolor()---------------------------------------//
//li单击可更改项目颜色
函数lichangecolor(){
$(“li”)。单击(函数(){
$(this.css(“颜色”、“红色”);
});
}
//--------------------函数deletenodes()--------------------------//
//函数删除节点
函数deletenodes(){
//删除此项以便删除
ministry=document.getElementById('minID').parentNode;
ministry.parentNode.removeChild(ministry);
//删除此链接以删除新闻链接
newslins=document.getElementById('relhead').parentNode;
newslins.parentNode.removeChild(新闻林);
//通过删除删除附近的新闻链接,将其删除
newslinksadj=document.getElementById('relhead').parentNode.parentNode.getElementsByTagName('td')[9];
newslinksadj.parentNode.removeChild(newslinksadj);
}
//----------------函数$()-----------------------//
//用于检测下拉列表变化的功能
(函数($){
$.fn.selected=函数(fn){
返回此值。每个(函数(){
var clicknum=0;
$(此)。单击(函数(){
clicknum++;
如果(clicknum==2){
clicknum=0;
fn(本);
}
});
});
}
})(jQuery);
//------------匿名函数()---------//
//论部门变动
$('#minID')。选中(函数(){
设置超时(
函数()
{
contentreplace();
lichangecolor();
}, 900);
});
//关于更改日期下拉列表
$('#rdateID')。选中(函数(){
设置超时(
函数()
{
contentreplace();
lichangecolor();
}, 900);
});
//关于月份变化下拉列表
$('#rmonthID')。选中(函数(){
设置超时(
函数()
{
contentreplace();
lichangecolor();
}, 900);
});
//关于年度变化下拉列表
$('#RyerId')。选中(函数(){
设置超时(
函数()
{
contentreplace();
lichangecolor();
}, 900);
});
很难从页面脚本中找到并修改XMLHttpRequest实例
但是还有另一种方法,监听dom更改,然后重新运行replace函数。你可以用
下面是用于侦听存档列表更改的代码片段
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});