跨多个页面的 tampermonkey 脚本 运行
tampermonkey script run accross multiple pages
这是我想做的一个例子。
每当我们达到目标时 url 即 Whosebug 会在底部出现一个带有按钮的粘性页脚。
在搜索中输入内容的按钮之一,提交表单。在此之后它等待页面加载并在刚刚加载的页面上做一些事情,即点击第一个 link.
我发现仅 运行 提交后点击是不可能的,因为页面的框架发生了变化或类似的事情,怎么可能用 tampermonkey 做到这一点。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://whosebug.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://whosebug.com/
// @grant none
// ==/UserScript==
// @match
仅匹配不包含搜索页的 whosebug.com 主页,因此 @match 应该是 // @match https://whosebug.com/*
,它与主页匹配和搜索页面。您还应该使用 if
语句使 "Click Me!" 按钮仅显示在主页上。
如果你愿意,我也完全解决了你的问题。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://whosebug.com/*
// @grant none
// ==/UserScript==
window.onload = function() {
'use strict';
if (location.href == "https://whosebug.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'
});
}
};
这是我想做的一个例子。 每当我们达到目标时 url 即 Whosebug 会在底部出现一个带有按钮的粘性页脚。 在搜索中输入内容的按钮之一,提交表单。在此之后它等待页面加载并在刚刚加载的页面上做一些事情,即点击第一个 link.
我发现仅 运行 提交后点击是不可能的,因为页面的框架发生了变化或类似的事情,怎么可能用 tampermonkey 做到这一点。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://whosebug.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://whosebug.com/
// @grant none
// ==/UserScript==
// @match
仅匹配不包含搜索页的 whosebug.com 主页,因此 @match 应该是 // @match https://whosebug.com/*
,它与主页匹配和搜索页面。您还应该使用 if
语句使 "Click Me!" 按钮仅显示在主页上。
如果你愿意,我也完全解决了你的问题。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://whosebug.com/*
// @grant none
// ==/UserScript==
window.onload = function() {
'use strict';
if (location.href == "https://whosebug.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'
});
}
};