2010-05-26 29 views
2

我需要帮助获取脚本,该脚本将从原始链接中提取参数并将它们重新写入新链接。我想这应该是非常容易的,但我仍然是一个noob,当谈到这一点。Greasemonkey:根据param重写所有链接

这是1链接的原始HTML代码。 (应globaly在页面上进行更换。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的链接来完成全球“/预览/”

感谢布鲁克亚当斯我有点了解如何获得这个代码的参数值,但我仍然没有真正知道如何重新编写页面中的所有链接。

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'; 

链接如果有人能这么好的帮我建立的示例脚本Greasemonkey的我将是非常感激。

回答

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); 
+0

再次感谢你这么多布鲁克·亚当斯。它适用于我。 特别花时间添加额外的信息。 :) – Bulfen 2010-05-28 10:34:59