2013-04-21 28 views
0

例如,我试图改变这一点:如何从一些链接中删除JavaScript阻止?

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

进入这个:

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

我需要字符串 “pcxe7gwP”,也就是目前的

key="pcxe7gwp" 

,然后一部分我想将它附加到URL的一部分

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

,并且,作为href代替当前

"javascript: void(null)" 

我使用Tampermonkey Chrome扩展,并试图建立一个userscript的做到这一点使用。我是新手,并会喜欢任何帮助。谢谢!

回答

0

测试在Greasemonkey的,不需要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'); 
} 
+0

你可能不需要* jQuery,就像你不需要*一个很好的无绳演习。但两者都使工作变得更容易。 jQuery是鼓励初学者进入的极好习惯。 – 2013-04-22 05:12:05

+0

我的意思是我的版本不需要jquery,让提问者参考,不告诉别人不要用jquery。 – muzuiget 2013-04-22 05:32:37

+0

muziget,这个作品很棒!谢谢! – 2013-04-22 05:50:25

0

如果我的理解没错,这是你在找什么:

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

另一种可能的解决方案:

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

好了,还有其他的解决方案,以实现相同的结果。但是,我认为这是脱节的。

干杯

+0

第一部分将不起作用,因为OP不控制页面。这是一个内容/用户脚本问题。来自第二部分的JS *代码*看起来可行,但对于脚本来说打包不正确。如果你愿意,我可以调整你的答案(然后提升它)。 – 2013-04-21 06:49:24

+0

@BrockAdams,我明白你的意思了。我没有意识到这个问题需要Tampermonkey或Greasemonkey的解决方案,因此我写了一个常规的HTML/Javascript代码。感谢您提供帮助。无需修复我的答案,因为我认为你的答案已经正确:) – Rafa 2013-04-21 14:45:47

+0

谢谢!我使用muzuiget的解决方案,因为这是我尝试的第一个解决方案。我相信你的也是。 – 2013-04-22 05:52:41

0

这里是一个完整的脚本,将在任一Tampermonkey或Greasemonkey的工作。它使用的易用性和动力的jQuery

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

谢谢!我使用muzuiget的解决方案,因为这是我尝试的第一个解决方案,但我非常感谢您的解决方案。 – 2013-04-22 05:53:17

+0

不客气。但是选择一个答案是因为这是你尝试工作的第一个答案并不明智,请参阅[FAQ](http://meta.stackexchange.com/a/5235/148310),尤其是这个指导:''确保除了为你工作之外,答案是非常好的做法,有时在答案被接受后,另一个人进来,发现前一个实际上是一个糟糕的黑客事实。 – 2013-04-23 00:14:40