沙盒是否为Google Chrome扩展"内容"脚本?

Jar*_*yth 6 javascript google-chrome sandbox xmlhttprequest

我的印象是content_scripts是在页面上执行的,但现在好像有一些沙盒正在进行中.

我正在开发一个扩展来记录站点的所有XHR流量(用于调试和其他开发目的),并且在控制台中,以下嗅探代码可以工作:

 var o = window.XMLHttpRequest.prototype.open;
 window.XMLHttpRequest.prototype.open = function(){
     console.log(arguments, 'open');
     return o.apply(this, arguments);
 };
 console.log('myopen');
 console.log(window, window.XMLHttpRequest, window.XMLHttpRequest.prototype, o, window.XMLHttpRequest.prototype.open);
Run Code Online (Sandbox Code Playgroud)

每次发送XHR时都会记录一条消息.然而,当我把它放在扩展中时,真正的原型不会被修改.显然我的脚本看到的window.XMLHttpRequest.prototype与实际页面的不同.

这有什么办法吗?此外,这个沙箱行为是否记录在任何地方?我环顾四周,却找不到任何东西.

190*_*Man 8

虽然Chrome的内容脚本位于"孤立的世界"中,但您可以通过将脚本元素插入dom来完成类似于您所要求的操作.

作为概念验证,我使用了TamperMonkey Chrome扩展程序并创建了此脚本:

// ==UserScript==
// @name         Modify Secret
// @namespace    http://your.homepage/
// @version      0.1
// @description  enter something useful
// @author       You
// @match        https://*/*
// @match        http://*/*
// @grant        none
// ==/UserScript==

console.log(secret);

var el = document.createElement('script');
el.innerHTML = 'secret = "the blue dog"';
document.body.appendChild(el);
Run Code Online (Sandbox Code Playgroud)

然后我导航到运行此javascript的http://s.codepen.io/boomerang/745009c49e60974cf9dba1b070f27d111458064000840/index.html:

var secret = 'the brown fox';

var secretPrinter = setInterval(function () {
    console.log(secret);
}, 1000);
Run Code Online (Sandbox Code Playgroud)

如果你检查控制台,人们会期望看到"棕色狐狸"不断打印,而是我们有"蓝狗".


总的来说,我认为浏览器试图实现的安全概念是防止页面环境访问内容脚本的环境.意识到,使用浏览器扩展可以完成类似的工作并不奇怪.


Moh*_*our 6

你不能这样做.根据文件:

但是,内容脚本有一些限制.他们不能:

  • 使用chrome.*API(chrome.extension的部分除外)
  • 使用其扩展页面定义的变量或函数
  • 使用由网页或其他内容脚本定义的变量或函数
  • 制作跨站点XMLHttpRequests

  • 真可惜啊.谢谢你找到了.如果只有chrome在它的扩展框架中有更多*power*. (3认同)