我需要帮助获取一个脚本,该脚本将从原始链接中获取参数并将其重新写入新链接。我想这应该很容易,但是当谈到这一点时,我仍然是一个菜鸟。
以下是1个链接的原始HTML代码。 (应该在页面上全局替换.image1.jpg,image2.jpg等。)
<div align="center"><a href="/index.php?Submit=ok&seson=b1e4&connect=127.0.0.1&id=13033&name=on"><img src="/preview/image1.jpg" width="128" height="128" border="0" style="border: 0px black solid;" /></a></div>
这应该在包含imagepath“/ preview /”
的所有链接上全局完成感谢Brock Adams,我有点了解如何使用此代码获取param值,但我仍然不知道如何重新编写页面中的所有链接。
var searchableStr = document.URL + '&';
var value1 = searchableStr.match (/[\?\&]id=([^\&\#]+)[\&\#]/i) [1];
var value2 = searchableStr.match (/[\?\&]connect=([^\&\#]+)[\&\#]/i) [1];
然后用“newlink”
重写链接var domain = searchableStr.match (/\/\/([w\.]*[^\/]+)/i) [1];
var newlink = '//' + domain + '/' + value1 + '/data/' + value2 + '.ext';
如果有人能帮助我设置一个示例性的milmonkey脚本,我会非常感激。
答案 0 :(得分:2)
好的,这是一个相当常见的任务,我没有看到任何先前的Stack Overflow问题 - 至少在2分钟的搜索中。
所以,根据提供的信息,这里有一个应该做你想要的脚本......
// ==UserScript==
// @name Site_X, image relinker.
// @namespace StackOverflow
// @description Rewrites the preview links to ???
// @include http://Site_X.com/*
// @include http://www.Site_X.com/*
// @include https://Site_X.com/*
// @include https://www.Site_X.com/*
// ==/UserScript==
function LocalMain () {
/*--- Get all the images that have /preview/ in their path.
*/
var aPreviewImages = document.evaluate (
"//img[contains (@src, '/preview/')]",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);
var iNumImages = aPreviewImages.snapshotLength;
GM_log (iNumImages + ' preview images found.');
/*--- Rewrite the parent links to our new specifications.
Note, all the target links are of the form:
<a href="/index.php?Submit=ok&seson=b1e4&connect=127.0.0.1&id=13033&name=on">
<img src="/preview/image1.jpg" width="128" height="128" border="0" style="border: 0px black solid;" />
</a>
The desired rewrite changes the link to this form:
<a href="{current page's domain}/{id-value}/data/{connect-value}.ext">
*/
for (var iLinkIdx=0; iLinkIdx < iNumImages; iLinkIdx++) {
var zThisImage = aPreviewImages.snapshotItem (iLinkIdx);
var zParentLink = zThisImage.parentNode;
//--- Get the key href parameters.
var sIdValue = sGetUrlParamValue (zParentLink, 'id');
if (!sIdValue) continue; //-- Oopsie, this link was a malformed.
var sConnectValue = sGetUrlParamValue (zParentLink, 'connect');
if (!sConnectValue) continue;
//--- Get the current page's domain. (Or just use a relative link.)
var sPageDomain = document.URL.match (/\/\/([w\.]*[^\/]+)/i) [1];
//--- Generate the desired link value.
var sDesiredLink = 'http://' + sPageDomain + '/' + sIdValue + '/data/' + sConnectValue + '.ext';
//--- Rewrite the target link.
zParentLink.href = sDesiredLink;
}
}
function sGetUrlParamValue (zTargLink, sParamName) {
var zRegEx = eval ('/[\?\&]' + sParamName + '=([^\&\#]+)[\&\#]/i');
var aMatch = (zTargLink.href + '&').match (zRegEx);
if (aMatch)
return decodeURI (aMatch[1]);
else
return null;
}
window.addEventListener ("load", LocalMain, false);