这是我想做的一个例子。
每当我们位于目标网址(即 stackoverflow)时,底部就会出现一个带有按钮的粘性页脚。
在搜索中输入内容的按钮之一是提交表单。之后,它等待页面加载并执行刚刚加载的页面的操作,即单击第一个链接。
我发现仅在提交后单击一下是不可能的,因为页面的框架发生了变化或类似的情况,如何使用 tampermonkey 来做到这一点。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://stackoverflow.com/
// @grant none
// ==/UserScript==
(function() {
'use strict';
$('body').append('<div id="test"><p><button onclick="myFunction()">Click me</button></p></div>');
$('body').append(
`<script type="text/javascript">
function myFunction() {
// page 1
document.querySelector('#search > div > input').value="tampermonkey";
document.forms[0].submit();
// page 2 (DOEST WORK)
document.querySelector('#question-summary-29592068 > div.summary > div.result-link > h3 > a');
}
</script>`
);
$('#test').css({'position': 'fixed',
'left': '0',
'bottom': '0',
'width': '100%',
'background-color': 'red',
'color': 'white',
'text-align': 'center',
'z-index':'1'
});
})();
您的脚本可能无法工作的原因之一是因为在脚本的顶部写着
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://stackoverflow.com/
// @grant none
// ==/UserScript==
The // @match
仅匹配 stackoverflow.com 主页,该主页不包含搜索页面,因此 @match 应该是// @match https://stackoverflow.com/*
它确实匹配主页和搜索页面。您还应该将其设置为“Click Me!”按钮仅通过使用显示在主页上if
陈述。
如果你愿意的话,我也完全解决了你的问题。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://stackoverflow.com/*
// @grant none
// ==/UserScript==
window.onload = function() {
'use strict';
if (location.href == "https://stackoverflow.com/search?q=tampermonkey") {
alert(document.querySelector('#question-summary-29592068 > div.summary > div.result-link > h3 > a'));
}else {
$('body').append('<div id="test"><p><button onclick="myFunction()">Click me</button></p></div>');
$('body').append(
`<script type="text/javascript">
function myFunction() {
// page 1
document.querySelector('#search > div > input').value="tampermonkey";
document.forms[0].submit();
}
</script>`
);
$('#test').css({
'position': 'fixed',
'left': '0',
'bottom': '0',
'width': '100%',
'background-color': 'red',
'color': 'white',
'text-align': 'center',
'z-index':'1'
});
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)