Events 如何从chrome用户脚本截取pjax页面更改?
我是否可以检测chrome用户脚本(不是扩展名)中的Events 如何从chrome用户脚本截取pjax页面更改?,events,google-chrome,userscripts,pushstate,pjax,Events,Google Chrome,Userscripts,Pushstate,Pjax,我是否可以检测chrome用户脚本(不是扩展名)中的历史记录.pushStateurl更改,而无需轮询(或打破脚本的孤立世界以拦截网页javascript上下文中的调用)? 我的问题,更详细地说: As使用history.pushState[]通过[]进行站点内页到页的站点导航,而不是在浏览站点时重新加载整个页面,只有在您以该url输入站点或手动强制重新加载页面时,针对站点上特定url的用户脚本才会触发 我正在为DRY编写一个小的用户脚本库:截取用户脚本,通过截取pushState调用来为他们处
历史记录.pushState
url更改,而无需轮询(或打破脚本的孤立世界以拦截网页javascript上下文中的调用)?
我的问题,更详细地说:
As使用history.pushState
[]通过[]进行站点内页到页的站点导航,而不是在浏览站点时重新加载整个页面,只有在您以该url输入站点或手动强制重新加载页面时,针对站点上特定url的用户脚本才会触发
我正在为DRY编写一个小的用户脚本库:截取用户脚本,通过截取pushState
调用来为他们处理这类事情,以查看我们何时在主机脚本想要运行的新页面上,并且只在相关的地方调用它
如果调用history.pushState
时触发事件,这将相对容易,但我能告诉你的是,除了加载第一页或用户触发历史上的前后移动外,不会触发任何popstate
事件,这对我来说是毫无用处的
我也尝试过将其限制为pjax/github专用解决方案–当加载新页面时,github会触发$(document.trigger('pageUpdate')
事件,但由于jQuery
在内部实现了这些,而不是通过DOM事件,我听不到来自我的用户脚本的消息,除非我在页面本身中插入一个小侦听器,让它的jQuery
实例将此事件报告给我,否则我可能会劫持页面的history.pushState
本身
有没有人看到其他整洁的方法来实现这一点?目前,我正在采用后一种方法,即注入一个截取
历史记录的脚本。一旦调用完成,pushState将调用并在文档上激发一个同名事件(可选地,在pjaxpageUpdate
事件触发后):
在GreaseMonkey更改其沙盒策略之前(读:Firefox更改了,GM没有使其向后兼容),可以使用unsafeWindow
获取元素/窗口事件。现在更改了,unsafeWindow在与一起使用时不再工作
您建议的向页面中注入脚本的答案在Github上不起作用,因为它的内容安全策略(CSP)非常严格,不允许内联脚本
内容安全策略:默认src*;脚本src assets-cdn.github.com collector-cdn.github.com;[…]
我的解决方案有两种:一种是使用@grant none
,另一种是使用GreaseMonkey API@grant
测试授予none.user.js
// ==UserScript==
// @name test @grant none
// @namespace https://github.com/jerone/UserScripts
// @include https://github.com/*
// @version 1
// @grant none
// ==/UserScript==
unsafeWindow.$(document).on("pjax:end", function() {
console.log('test @grant none 1');
});
unsafeWindow.$(window).on('popstate', function() {
console.log('test @grant none 2');
});
// ==UserScript==
// @name test @grant GM_*
// @namespace https://github.com/jerone/UserScripts
// @include https://github.com/*
// @version 1
// @grant GM_setValue
// ==/UserScript==
unsafeWindow.$(document).on("pjax:end", exportFunction(function() {
console.log('test @grant GM_* 1');
}, unsafeWindow));
unsafeWindow.$(window).on('popstate', exportFunction(function() {
console.log('test @grant GM_* 2');
}, unsafeWindow));
test-grant-GM.user.js
// ==UserScript==
// @name test @grant none
// @namespace https://github.com/jerone/UserScripts
// @include https://github.com/*
// @version 1
// @grant none
// ==/UserScript==
unsafeWindow.$(document).on("pjax:end", function() {
console.log('test @grant none 1');
});
unsafeWindow.$(window).on('popstate', function() {
console.log('test @grant none 2');
});
// ==UserScript==
// @name test @grant GM_*
// @namespace https://github.com/jerone/UserScripts
// @include https://github.com/*
// @version 1
// @grant GM_setValue
// ==/UserScript==
unsafeWindow.$(document).on("pjax:end", exportFunction(function() {
console.log('test @grant GM_* 1');
}, unsafeWindow));
unsafeWindow.$(window).on('popstate', exportFunction(function() {
console.log('test @grant GM_* 2');
}, unsafeWindow));
您可以在此处找到以上脚本:
您可以在这里阅读更多内容:
感谢Athorcis为我指明了正确的方向。因此,我在搜索后再次找到了你的问答,因为我以前的代码不再适用于对沙箱的GreaseMonkey(阅读Firefox)更改。你的代码将失败,因为Github收紧了CSP,禁止内联JavaScript。请参阅其他回复以获得解决方案。