[英]JS library that “reloads” a page to mimic pre-load code injection?
Apologies if the title is vague, but it's not the easiest question to describe. 如果标题含糊,则表示歉意,但这并不是最容易描述的问题。 Currently, if you want to wrap/overload a built-in function in the browser, such as XmlHttpRequest.prototype.open = function(){return oldOpen(...)}
, and you want to do it before any scripts in the page execute, you'd need to use a browser extension/addon that executes before the document starts being parsed. 当前,如果您想在浏览器中包装/重载内置函数,例如XmlHttpRequest.prototype.open = function(){return oldOpen(...)}
,并且想要在浏览器中的任何脚本之前执行此操作页面执行,您需要使用浏览器扩展程序/附件,该扩展程序/附件会在文档开始解析之前执行。 The thing is, browser extensions often have a low conversion rate of users who agree to install them. 事实是,浏览器扩展程序通常同意安装它们的用户转换率较低。 I'd love to be able to use bookmarklets to do the same thing (and Pinterest has shown that bookmarklets aren't a barrier for them!). 我很希望能够使用小书签来做同样的事情(而且Pinterest已经表明小书签对他们没有任何障碍!)。 But alas, bookmarklets by definition run long after the document has loaded. 但是可惜,按定义,小书签会在文档加载后很长时间内运行。
What I'd like to do, and what I think is theoretically possible, is to fake a page reload, but with a script inserted already. 我想做的事以及我认为在理论上可能的事是对页面重新加载进行伪造,但已插入了脚本。 The process would look something like this: 该过程如下所示:
window.location
. 通过XHR从window.location
下载原始文档。 window
and document
that aren't there in a blank document... which, given that there might be some extensions installed, might require some iframe trickery/messaging to actually get a reference blank document. 删除/取消定义空白文档中不存在的window
和document
中的所有键...鉴于可能已安装一些扩展名,因此可能需要一些iframe技巧/消息才能真正获得参考空白文档。 Now, I'd be perfectly willing to take a stab at this and open source it, but has anyone tried something like this before? 现在,我非常愿意对此采取行动并将其开源,但是以前有人尝试过这种方法吗? It'll be a lot of work, and I'd rather not reinvent the wheel unless what we're using now is the equivalent of square boulders on sticks. 这将需要大量工作,并且除非我们现在使用的等效于棍棒上的方形巨石,否则我不想重塑轮子。
Or rewrite the current url into a new window with your appropriate js shim. 或使用适当的js垫片将当前网址重写到新窗口中。
(function() {
var scriptSrc = 'http://example.com/shim.js';
var rq = new XMLHttpRequest; rq.open('get', '', false);
rq.send();
var text = rq.responseText.replace('<head', '<script src="' + scriptSrc + '"></script><head');
var win = window.open();
win.document.open();
win.document.write(text);
win.document.close();
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.