简体   繁体   English

可以“重新加载”页面以模仿预加载代码注入的JS库吗?

[英]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: 该过程如下所示:

  1. Download, via XHR, the original document from window.location . 通过XHR从window.location下载原始文档。
  2. Remove all elements from the DOM, including all the script tags. 从DOM中删除所有元素,包括所有脚本标签。 Maybe throw up a nice splash screen for a good user experience. 也许会抛出一个漂亮的启动屏幕以获得良好的用户体验。
  3. Clear all timeouts and intervals (on FF and Chrome the IDs seem to count up from 1, so no a priori knowledge is needed AFAIK). 清除所有超时和间隔(在FF和Chrome上,ID似乎从1开始计数,因此AFAIK不需要先验知识)。
  4. Delete/undefine all keys in 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. 删除/取消定义空白文档中不存在的windowdocument中的所有键...鉴于可能已安装一些扩展名,因此可能需要一些iframe技巧/消息才能真正获得参考空白文档。
  5. Inject your scripts into the now-blank document. 将脚本注入到空白文档中。
  6. Re-insert all the tags from the XHR'd original document, including the scripts, which should then reload and re-execute in your new context. 重新插入XHR原始文档中的所有标签,包括脚本,然后应在新的上下文中重新加载并重新执行。
  7. Take down the splash screen, and you're good to go! 放下启动画面,您就该走了!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM