2013-04-21 25 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