如何从一些链接中删除javascript块?
例如,我试图改变这一点:如何从一些链接中删除javascript块?,javascript,greasemonkey,tampermonkey,Javascript,Greasemonkey,Tampermonkey,例如,我试图改变这一点: <a href="javascript: void(null)" class="jv-redirectCandidate" key="pcxe7gwP" >Some Name</a> 然后我想把它附加到URL的一部分 https://www.foo.com/something.aspx?p= 并将其用作href以代替当前 "javascript: void(null)" 我正在使用Tampermonkey Chrome扩展,并试图
<a href="javascript: void(null)" class="jv-redirectCandidate"
key="pcxe7gwP"
>Some Name</a>
然后我想把它附加到URL的一部分
https://www.foo.com/something.aspx?p=
并将其用作href
以代替当前
"javascript: void(null)"
我正在使用Tampermonkey Chrome扩展,并试图创建一个用户脚本来实现这一点。我不熟悉用户脚本,希望能得到任何帮助。谢谢 如果我理解正确,这就是你要找的:
<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>
函数更改href(元素){
element.href=”https://www.foo.com/something.aspx?p=“+element.key;
}
另一种可能的解决办法:
<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>
函数changeHREF(){
元素=document.getElementsByClassName(“jv候选”);
对于(i=0;i,这里有一个完整的脚本可以在Tampermonkey或Greasemonkey中使用。它的使用是为了方便和强大:
// ==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);
} );
在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');
}
/==UserScript==
//@name Change link href及其密钥
//@命名空间测试
//@grant none
//@version 1
//@包括http://localhost:8000/*.html
//==/UserScript==
var前缀=https://www.foo.com/something.aspx?p=';
var links=document.querySelectorAll('a.jv-redirectCandidate[key]');
对于(变量i=0;i
提示,提示:
有一个类jv Redirect候选者
。使用该类对所有成员进行操作。第一部分不起作用,因为OP无法控制页面。这是一个内容/用户脚本问题。第二部分的JS代码看起来可行,但打包的脚本不正确。如果您需要,我可以调整您的答案(然后向上投票)。@BrockAdams,我明白你的意思。我不知道这个问题需要一个针对Tampermonkey或Greasemonkey的解决方案,因此我编写了一个常规HTML/Javascript代码。感谢你提供的帮助。无需修改我的答案,因为我认为你的答案已经正确:)谢谢!我使用了muzuiget的解决方案,因为这是我第一次尝试,而且效果很好。我相信你的解决方案也很好。你可能不需要jQuery,就像你不需要一个好的无绳训练一样。但两者都让工作更容易。jQuery是鼓励初学者养成的一个好习惯。我的意思是,我的版本不需要jQuery,让asker参考,不是告诉其他人不要使用jquery。谢谢!我选择了muzuiget的解决方案,因为这是我第一次尝试的解决方案,但我真的很感激你的解决方案。不客气。但是选择一个答案,因为这是你第一次尝试的解决方案,是不明智的,请参阅,特别是以下指南:“除了为你工作之外,还要确保答案是非常好的做法。有时候,在答案被接受后,另一个会进来,发现前一个其实是一个糟糕的黑客。”
。
// ==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);
} );
// ==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');
}