Greasemonkey 脚本能否将以下脚本的 Name
变量与另一个站点的成员列表进行比较,并使其运行脚本的其他部分(如果 Name
不这样做)?与任何人都不匹配?
//alert(Name);
var postHistory = "http://"+regionSlice+".targetforum.com/board/search.php?do=process&searchuser="+Name+"&exactname=1&showposts=1";
var avatar = "http://othersite.com/forum/avatar/" +regionSlice+ "." + Name + ".png"; // creates an avatar
// Replace the old Avatar
$('.user_icon', this).attr('src', avatar);
$('.user_icon', this).wrap('<a href="' + postHistory + '" class="link"></a>');
$('.user_icon', this).attr('width', '80');
$('.user_icon', this).attr('height', '80');
$('.user_icon', this).attr('style', 'position:relative; TOP:7px');
如果 Name
不匹配任何记录,这段代码将执行下面的部分,那就完美了。
var postHistory = "http://"+regionSlice+".targetforum.com/board/search.php?do=process&searchuser="+Name+"&exactname=1&showposts=1";
var avatar = "http://othersite.com/forum/avatar/" +regionSlice+ "." + Name + ".png"; // creates an avatar
var noAvatar = "http://other.com/forum/avatar/questionmark.png";
// Replace the old Avatar
$('.user_icon', this).attr('src', noAvatar);
$('.user_icon', this).wrap('<a href="' + postHistory + '" class="link"></a>');
$('.user_icon', this).attr('width', '80');
$('.user_icon', this).attr('height', '80');
$('.user_icon', this).attr('style', 'position:relative; TOP:7px');
<小时/>
othersite.com/forum/members.php 的成员列表:
<?php
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$result = mysql_query("SELECT username FROM users ORDER BY ID");
echo "<table border='0'>
<tr>
<th>UserName</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<小时/>
这是该页面的示例部分。该部分在一页中出现 1 到 10 次,因此 Name
将被检查 1 到 10 次。 “Vortexer”是 Name
的一个示例,下面两行是 user_icon
。
<div id="edit4767039" style="padding:0px 0px 6px 0px">
<a name="4767039"> </a>
<div class="forum_post post_frame" id="post4767039">
<div class="post_hidden_message">
Comment below rating threshold, click <a href="#">here</a> to show it.
</div>
<table>
<tr>
<td class="left" valign="top" id="currentPost">
<div class="avatar_top">
<div class="avatar" style="padding-top:10px;">
<big>Vortexer</big>
<a class="photo">
<img class="user_icon" src="theme/img/unknown_icon.jpg"/>
<span class="left_orb">??</span>
<span class="right_orb"><img src="http://irrelevantserver.com/forum/ui/avatar_right_orb_blue.png" alt=""/></span>
</a>
<small>Senior Member</small>
</div>
<center>
<a href="//articles/The_Code" target="_blank"><span class="sca_icon" style="align: center"><img src="http://irrelevantserver.com/images/community/community site/SCA_badge.png" alt="This user has accepted the code, click for more information"/></a></span>
</center>
</div>
</td>
<td class="right" valign="top">
<table class="right_table">
<tr>
<td>
<div class="message_header">
<!-- status icon and date -->
<span>1 Day Ago</span>
<!-- / status icon and date -->
</div>
<div class="post_content" id="post_message_4767039">
<p>This is forum post content</p>
</div>
<div class="message_footer">
<div class="r_block">
<span class="post_rating">
<span class="rating_positive">+3</span>
</span>
<a href="ratepost.php?postid=4767039&vote=-1" rel="nofollow" class="vote_down_button">
<img src="http://irrelevantserver.com/forum/ui/thumbs_down.png" alt=""/>
</a>
<a href="ratepost.php?postid=4767039&vote=1" rel="nofollow" class="vote_up_button">
<img src="http://irrelevantserver.com/forum/ui/thumbs_up.png" alt=""/>
</a>
<a href="newreply.php?do=newreply&p=4767039" class="quick-reply" rel="nofollow"><img src="http://irrelevantserver.com/forum/ui/message_quote_icon.png" alt="Reply With Quote"/></a>
<a href="editpost.php?do=editpost&p=4767039" name="vB::QuickEdit::4767039"><img src="http://irrelevantserver.com/forum/ui/edit_icon.png" alt="Edit/Delete Message" class="edit_button"/></a>
</div>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
使用以下代码获取名称:
// Replace everypost's avatar
$('.forum_post').each(function(index) {
name = $('big', this).html();
//alert($('big', this).html());
var Name1 = name.replace("\<font ", "");
var Name2 = Name1.replace("color\=\"\#c98f1a\"\>", "");
var Name3 = Name2.replace("color=\"green\"\>", "");
var Name4 = Name3.replace("color=\"red\"\>", "");
var Name = Name4.replace("</font>", "");
最佳答案
是的,您可以将名称
与从其他网页抓取的内容进行比较。由于看起来是跨域的,所以必须使用 GM_xmlhttpRequest
来做到这一点。
从 members.php 的外观来看,它返回一个如下表:
<table>
<tr><th>UserName</th></tr>
<tr><td>User A</td></tr>
<tr><td>User B</td></tr>
<tr><td>User C</td></tr>
<tr><td>User D</td></tr>
</table>
没有别的。
(如果它确实返回其他内容,则需要调整以下代码中的选择器。)
我做了a mockup of that HTML at jsFiddle .
如果您安装此 Greasemonkey 脚本,您可以看到它读取用户名:
// ==UserScript==
// @name _Parse simple AJAX page scrape/fetch
// @include http://stackoverflow.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
GM_xmlhttpRequest ( {
method: 'GET',
url: 'http://fiddle.jshell.net/TDcDV/show/',
onload: getUsernamesFromAjax
} );
function getUsernamesFromAjax (respObject) {
var respDoc = $(respObject.responseText);
var userNameTDs = respDoc.find ("td");
var userNames = userNameTDs.map ( function () {
return this.textContent;
} ).get ();
alert ("The usernames are: " + userNames);
}
(警报,“用户名是:用户 A、用户 B、用户 C、用户 D
”。)
现在,至于将Name
与数据进行比较,问题还不清楚。
名称
来自哪里?- 是否有多个
Name
值?如果是,哪个.user_icon
节点与哪个Name
相关?
链接或pastebin到整个页面。
<小时/>更新:
根据新信息,您希望脚本执行以下操作:
用“进行中”图像(“throbber”)替换头像,让用户知道我们正在等待 AJAX 结果。
(AJAX 可能需要几秒钟。)向外部站点发起 AJAX 请求以获取成员列表。
当 AJAX 返回成员列表时:
- 对于成员列表中的姓名,请替换其头像。
- 对于不在成员列表中的姓名,请将其头像替换为“ headless 像”图像。
这是执行此操作的完整脚本。
安装脚本,然后访问jsbin.com/awaxap/1 ,看看它的实际效果。
// ==UserScript==
// @name _Replace avatars for matching names
// @include http://jsbin.com/awaxap/*
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @resource waitImg http://i.cdn.turner.com/money/.element/img/1.0/misc/throbber.gif
// @grant GM_xmlhttpRequest
// @grant GM_getResourceURL
// ==/UserScript==
//--- Replace avatars with an "In progress" image.
var waitImgSrc = GM_getResourceURL ("waitImg");
$("div.forum_post div.avatar img.user_icon").attr ('src', waitImgSrc);
//--- Fetch the member list.
GM_xmlhttpRequest ( {
method: 'GET',
url: 'http://fiddle.jshell.net/TDcDV/show/',
onload: changeUserIconsOfMembers
} );
//--- Replace avatars based on name match.
function changeUserIconsOfMembers (respObject) {
var respDoc = $(respObject.responseText);
var userNameTDs = respDoc.find ("td");
var userNames = userNameTDs.map ( function () {
return $.trim (this.textContent.toLowerCase () );
} ).get ();
//--- Replace every post's avatar.
$('div.forum_post').each ( function (index) {
//-- text() automatically strips out any <font> cruft, if present.
var Name = $('div.avatar big', this). text ();
//-- Standardize name for comparison.
Name = $.trim (Name).toLowerCase ();
var regionSlice = ""; // where's this come from?
var postHistory = "http://" + regionSlice
+ ".targetforum.com/board/search.php?do=process&searchuser="
+ Name + "&exactname=1&showposts=1"
;
var avatar = "http://othersite.com/forum/avatar/" + regionSlice
+ "." + Name + ".png"
; // creates an avatar
avatar = "/image/Nrzn7.jpg"; // Temp avatar upgrade. ;)
//--- Was the username not found?
if (userNames.indexOf (Name) === -1 ) {
avatar = "http://other.com/forum/avatar/questionmark.png";
avatar = "/image/BbOsC.gif"; // Temp avatar upgrade. ;)
}
//--- Replace the old Avatar and give it a link to history.
var userIcon = $('div.avatar img.user_icon', this);
userIcon.attr ( {
src: avatar,
width: '80',
height: '80',
style: 'position:relative; TOP:7px;'
} );
userIcon.wrap ('<a href="' + postHistory + '" class="link"></a>');
} );
}
请注意,未找到用户“Vortexer”,但找到了“User B”。
关于php - Greasemonkey 脚本将 JavaScript 变量与另一个页面的内容进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12555408/