Javascript 如何让一些代码只在“.html”网页上运行?
我有一些代码,我想在Javascript 如何让一些代码只在“.html”网页上运行?,javascript,jquery,greasemonkey,tampermonkey,Javascript,Jquery,Greasemonkey,Tampermonkey,我有一些代码,我想在mywebsite.com/x.html (其中x可以是任何内容) 下面的代码(我希望)将在网页上找到ID为“mybutton”的按钮,并在延迟1秒后自动单击该按钮: <script> window.onload = function() { setTimeout('document.getElementById('mybutton').click()', 1000); } </script> window.onload=函数(
mywebsite.com/x.html
(其中x
可以是任何内容)
下面的代码(我希望)将在网页上找到ID为“mybutton”的按钮,并在延迟1秒后自动单击该按钮:
<script>
window.onload = function() {
setTimeout('document.getElementById('mybutton').click()', 1000);
}
</script>
window.onload=函数(){
setTimeout('document.getElementById('mybutton')。click()',1000);
}
我在谷歌上搜索并找到了Tampermonkey Chrome扩展,但唯一的问题是我不知道如何只在上面提到的特定网页上运行它
我只是把我的代码放在Tampermonkey给出的示例布局的末尾,得到了以下结果:
// ==UserScript==
// @name My Fancy New Userscript
// @namespace http://your.homepage/
// @version 0.1
// @description enter something useful
// @author You
// @match http://website.com
// @grant none
// ==/UserScript==
<script>
window.onload = function() {
setTimeout('document.getElementById('mybutton').click()', 1000);
}
</script>
/==UserScript==
//@name我最喜欢的新用户脚本
//@名称空间http://your.homepage/
//@version 0.1
//@description输入一些有用的内容
//@author You
//@匹配http://website.com
//@grant none
//==/UserScript==
window.onload=函数(){
setTimeout('document.getElementById('mybutton')。click()',1000);
}
如何使其在开头提到的特定网页上运行?您可以使用星号作为通配符来定义Tampermonkey代码将在哪些网页上运行。
因此,它可以在您网站的任何子目录中工作:
// @match http://www.mywebsite.com/*
要使其仅适用于html页面,请更改通配符:
// @match http://www.mywebsite.com/*.html
您可以使用星号作为通配符来定义Tampermonkey代码将在哪些网页上运行。
因此,它可以在您网站的任何子目录中工作:
// @match http://www.mywebsite.com/*
要使其仅适用于html页面,请更改通配符:
// @match http://www.mywebsite.com/*.html
您可以使用以下代码通过纯javascript获取当前url段:
var url = window.location.pathname.split( '/' );
因此,如果您的链接是mywebsite.com/about,about将是url[1]
您可以定义要在其中执行代码的页面数组,然后使用当前url进行检查
var url = window.location.pathname.split( '/' );
var pages = ['home', 'about', 'services'];
for(var i=0; i<pages.length; i++) {
if(pages[i] === url[1]) {
// execute your code
}
}
var url=window.location.pathname.split('/');
var pages=['home','about','services'];
对于(var i=0;i,您可以使用以下代码通过纯javascript获取当前url段:
var url = window.location.pathname.split( '/' );
因此,如果您的链接是mywebsite.com/about,about将是url[1]
您可以定义要在其中执行代码的页面数组,然后使用当前url进行检查
var url = window.location.pathname.split( '/' );
var pages = ['home', 'about', 'services'];
for(var i=0; i<pages.length; i++) {
if(pages[i] === url[1]) {
// execute your code
}
}
var url=window.location.pathname.split('/');
var pages=['home','about','services'];
对于(var i=0;i几件事:
若要将操作限制为仅.html
页面,请检查位置.pathname
(见下文)
或者使用@包括。例如:
// @include http://website.com/*.html
对于精度和性能,我建议使用@match
而不是@include
几乎总是在@match
指令的末尾需要一个通配符。例如:
// @match http://website.com/*
请注意,通配符在@include
s中更灵活,但在@match
s中更“安全”,执行速度更快
不能像
那样将直接HTML标记放入用户脚本中。无论如何,在这种情况下根本不需要它
在用户脚本中很少需要窗口。onload
。在大多数情况下,默认情况下,用户脚本会在理想时间启动。
不要在setTimeout
中使用“eval”字符串。这会导致性能、故障排除和安全问题,如果它能正常工作的话。
问题的setTimeout
使用了其中的大部分,还有一个语法错误
直接的.click()
调用有时会起作用,而且。
如果不起作用,发送。
避免使用任意定时器。例如:设置超时(…,1000);
这是一种形式的“定时炸弹”代码。它有时会起作用。其他时候,页面会延迟。当它起作用时,它可能会大大降低脚本的速度。
使用。请参阅下面的代码
综上所述,使用jQuery和标准实用程序既可以节省工作又可以提高健壮性,您的脚本如下所示:
// ==UserScript==
// @name _Auto click the X button on Y pages
// @match http://website.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.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.
*/
//--- Does the *path* part of the URL end in `.html`?
if (/\.html$/i.test (location.pathname) ) {
waitForKeyElements ("#mybuttonID", clicktargetButton);
function clicktargetButton (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
}
有几件事:
若要将操作限制为仅.html
页面,请检查位置.pathname
(见下文)
或者使用@包括。例如:
// @include http://website.com/*.html
对于精度和性能,我建议使用@match
而不是@include
几乎总是在@match
指令的末尾需要一个通配符。例如:
// @match http://website.com/*
请注意,通配符在@include
s中更灵活,但在@match
s中更“安全”,执行速度更快
不能像
那样将直接HTML标记放入用户脚本中。无论如何,在这种情况下根本不需要它
在用户脚本中很少需要窗口。onload
。在大多数情况下,默认情况下,用户脚本会在理想时间启动。
不要在setTimeout
中使用“eval”字符串。这会导致性能、故障排除和安全问题,如果它能正常工作的话。
问题的setTimeout
使用了其中的大部分,还有一个语法错误
直接的.click()
调用有时会起作用,而且。
如果不起作用,发送。
避免使用任意定时器。例如:设置超时(…,1000);
这是一种形式的“定时炸弹”代码。它有时会起作用。其他时候,页面会延迟。当它起作用时,它可能会大大降低脚本的速度。
使用。请参阅下面的代码
综上所述,使用jQuery和标准实用程序既可以节省工作又可以提高健壮性,您的脚本如下所示:
// ==UserScript==
// @name _Auto click the X button on Y pages
// @match http://website.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.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.
*/
//--- Does the *path* part of the URL end in `.html`?
if (/\.html$/i.test (location.pathname) ) {
waitForKeyElements ("#mybuttonID", clicktargetButton);
function clicktargetButton (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
}
首先从脚本文件中删除这些标记。if(window.locat