Javascript Userscript只在后端提供的页面上工作,但在前端不以SPA方式工作
我在Greasemonkey/Tampermonkey上运行了以下用户脚本 我在Javascript Userscript只在后端提供的页面上工作,但在前端不以SPA方式工作,javascript,ajax,console,single-page-application,userscripts,Javascript,Ajax,Console,Single Page Application,Userscripts,我在Greasemonkey/Tampermonkey上运行了以下用户脚本 我在facebook.com上运行它,它从后端启动一些网页,在前端通过HRO运行其他一些网页,就像单页应用程序(SPA)一样 如果我在控制台上运行此脚本,即使在基于HRO的网页中,它也可以正常运行,但从Greasemoneky/Tampermonkey运行时,它不会在这些特定网页中运行 如何使脚本在类似SPA的网页上也能正常工作?在这种情况下,当setTimeout、setInterval和事件委派本身无法工作时,可以将
facebook.com
上运行它,它从后端启动一些网页,在前端通过HRO运行其他一些网页,就像单页应用程序(SPA)一样
如果我在控制台上运行此脚本,即使在基于HRO的网页中,它也可以正常运行,但从Greasemoneky/Tampermonkey运行时,它不会在这些特定网页中运行
如何使脚本在类似SPA的网页上也能正常工作?在这种情况下,当
setTimeout
、setInterval
和事件委派本身无法工作时,可以将实现它们的状态推入内存,然后用它替换现有状态,这样网页的DOM内容就会改变
下面是一段代码,用于替换使用AJAX而不是直接从PHP加载的数据:
let utilityFunc = ()=> {
var run = (url)=> {
// insert your code here
};
var pS = window.history.pushState;
var rS = window.history.replaceState;
window.history.pushState = function(a, b, url) {
run(url);
pS.apply(this, arguments);
};
window.history.replaceState = function(a, b, url) {
run(url);
rS.apply(this, arguments);
};
utilityFunc();
这就是我从阅读中理解的
let utilityFunc = ()=> {
var run = (url)=> {
// insert your code here
};
var pS = window.history.pushState;
var rS = window.history.replaceState;
window.history.pushState = function(a, b, url) {
run(url);
pS.apply(this, arguments);
};
window.history.replaceState = function(a, b, url) {
run(url);
rS.apply(this, arguments);
};
utilityFunc();