0

For example, I am trying to change this:

<a href="javascript: void(null)" class="jv-redirectCandidate" 
    key="pcxe7gwP"
>Some Name</a>

Into this:

<a href="https://www.foo.com/something.aspx?p=pcxe7gwP">Some Name</a>

I need the string "pcxe7gwP" that is currently part of

key="pcxe7gwp"

and then I want to attach it to part of a URL

https://www.foo.com/something.aspx?p=

and the use that as the href in place of the current

"javascript: void(null)"

I am using the Tampermonkey Chrome extension and trying to create a userscript to accomplish this. I am new to userscripts and would love any help. Thanks!

4

3 回答 3

0

If I understood right, this is what you are looking for:

<html>
<script type="text/javascript">
function changeHREF(element){
    element.href = "https://www.foo.com/something.aspx?p=" + element.key;
}
</script>
<body>
<a href="#" onclick="javascript:changeHREF(this);" class="jv-redirectCandidate" key="pcxe7gwP" id="myId">Some Name</a>
</body></html>

Another possible solution:

<html>
<script type="text/javascript">
function changeHREF(){
    elements = document.getElementsByClassName("jv-redirectCandidate");
    for(i = 0; i<elements.length; i++) {
        elements[i].href = "https://www.foo.com/something.aspx?p=" + elements[i].getAttribute("key");
    }
}
</script>
<body onload="javascript:changeHREF()">
<a href="javascript:void(null);" class="jv-redirectCandidate" key="pcxe7gwP">Some Name</a>
</body></html>

Well, there are other solutions to achieve the same results. But, I think that it is out of topic.

Cheers

于 2013-04-21T06:21:48.060 回答
0

Here is a complete script that will work in either Tampermonkey or Greasemonkey. It use's jQuery for ease and power:

// ==UserScript==
// @name     _De-javascript links
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
//-- Get links with the class "jv-redirectCandidate".
var linksToFix = $("a.jv-redirectCandidate");

//-- Loop through the links
linksToFix.each ( function () {
    var jThis   = $(this);  //-- An individual link
    var key     = jThis.attr ("key");

    jThis.attr ("href", "https://www.foo.com/something.aspx?p=" + key);
} );
于 2013-04-21T06:46:12.230 回答
0

Test in Greasemonkey, don't need jquery.

// ==UserScript==
// @name        Change link href with it's key
// @namespace   test
// @grant       none
// @version     1
// @include     http://localhost:8000/*.html
// ==/UserScript==

var prefix = 'https://www.foo.com/something.aspx?p=';
var links = document.querySelectorAll('a.jv-redirectCandidate[key]');
for (var i = 0; i < links.length; i += 1) {
    var link = links[i];
    link.href = prefix + link.getAttribute('key');
}
于 2013-04-22T05:05:09.953 回答