简体   繁体   English

从chrome扩展的内容脚本动态注入jQuery后,为什么不能使用jQuery?

[英]Why can't I use jQuery after it's dynamically injected from a chrome extension's content-script?

When run as a script in the usual way on a page, this code prints the version of jQuery after it is dynamically injected into a page. 当以脚本的方式在页面上以常规方式运行时,此代码在动态注入页面后将打印jQuery的版本。 But in my chrome-extension, when I try to print jQuery's version after I dynamically inject it, the output is undefined . 但是在我的chrome扩展名中,当我在动态注入jQuery后尝试打印jQuery的版本时,输出是undefined

// create iframe
var frame = document.createElement("iframe");

// when frame loads
frame.addEventListener("load", function() {
  // create script to dynamically inject into DOM
  var script = frame.contentWindow.document.createElement("script");
  // when script loads print jquery version
  script.addEventListener("load", function() {
    console.log(frame.contentWindow.$.fn.jquery); // output -> 2.2.1
  });
  frame.contentWindow.document.head.appendChild(script);
  script.src = "https://code.jquery.com/jquery-2.2.1.js";
});

document.body.appendChild(frame);

So this is my problem: 所以这是我的问题:

I have to dynamically inject some code (I'm using jQuery as an example) into an iframe because I need it to have the correct context of the iframe.contentWindow as the window. 我必须向iframe中动态注入一些代码(我以jQuery为例),因为我需要它具有iframe.contentWindow作为窗口的正确上下文。

The problem is that whenever I try to use the global that is created with the a script such as someModule.js using frame.contentWindow.someModule (in this case it's frame.contentWindow.$ since I injected the jQuery source code) it is undefined. 问题是,每当我尝试使用通过诸如frame.contentWindow.someModule之类的someModule.js之类的脚本创建的全局frame.contentWindow.someModule (在本例中为frame.contentWindow.$因为我注入了jQuery源代码)时,它都是不确定的。 I think this is something to do with it being a chrome extension (like the chrome extension javascript is isolated from the other code) 我认为这与chrome扩展名有关(例如chrome扩展名javascript与其他代码隔离了)

My question: 我的问题:

  1. Why does this happen? 为什么会这样?

  2. How can I bypass (or any solution to this) it? 我如何绕过(或对此的任何解决方案)呢?
    2.1. 2.1。 How can I dynamically add JavaScript (a file, or some code) so that it's in the context of an iFrame (and not the parent window). 如何动态添加JavaScript(文件或某些代码),使其位于iFrame(而不是父窗口)的上下文中。 I must do it during run-time as it's a chrome extension with a content-script. 我必须在运行时执行此操作,因为它是带有内容脚本的chrome扩展程序。

Or can I possibly run the chrome extension content-script directly in the iframe? 还是可以直接在iframe中运行chrome扩展程序content-script?

Fiddle with the code above: https://jsfiddle.net/wj83oytd/5/ 摆弄上面的代码: https : //jsfiddle.net/wj83oytd/5/

Found the problem, 发现问题了

Content scripts execute in a special environment called an isolated world. 内容脚本在称为隔离世界的特殊环境中执行。 They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. 他们有权访问所注入页面的DOM,但不能访问该页面创建的任何JavaScript变量或函数。 It looks to each content script as if there is no other JavaScript executing on the page it is running on. 看起来每个内容脚本都好像在其上运行的页面上没有其他JavaScript在执行。 The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts. 反之亦然:在页面上运行的JavaScript无法调用任何函数或访问由内容脚本定义的任何变量。

https://developer.chrome.com/extensions/content_scripts#execution-environment https://developer.chrome.com/extensions/content_scripts#execution-environment

Solution, 解,

Dynamically inject the code you want to run as inline JavaScript code as well, instead of running it in the content-script 动态地将要运行的代码也作为内联JavaScript代码注入,而不是在内容脚本中运行

Code that doesn't work: 无效的代码:

 // calling it in the content-script
 console.log(frame.contentWindow.$.fn.jquery); // undefined

Solution: 解:

var script = frame.contentWindow.document.createElement("script");
script.innerHTML = "console.log(window.$.fn.jquery);"; // set script code
// adding it to the frame and running it in it's context
frame.contentWindow.document.body.appendChild(rmptipScript); // injects the code
// prints 2.2.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何动态添加或删除Chrome扩展程序,内容脚本,javascript文件? - How do I add or remove Chrome extension, content-script, javascript files dynamically? 将选项页面中的设置交换为Chrome扩展程序中的内容脚本 - Exchanging Settings from Options Page to Content-Script in Chrome Extension Chrome扩展程序的内容脚本如何确定注入页面的引荐来源网址? - How can a Chrome extension's content script determine the injected page's referrer? Chrome扩展程序的内容脚本未注入特定网站 - Chrome extension's content script not getting injected on specific websites 为什么我的 chrome 扩展内容脚本无法访问页面元素的属性? - Why can't my chrome extension content script have access to a page element's property? 如何在后台获取chrome扩展程序的内容脚本? - How can I get chrome extension's content script to in the background? 为什么我的内容脚本不能使用已经注入的脚本? - Why can't my content script use already injected script? 使用Chrome扩展程序内容脚本从localStorage中获取令牌值 - Getting a token value out of localStorage with a Chrome extension content-script Chrome扩展程序:如何在全局范围内捕获/处理内容脚本错误? - Chrome extension: how to trap/handle content-script errors globally? 如何为 URL 中的给定哈希触发 Chrome 扩展程序、内容脚本? - How to trigger a Chrome extension, content-script for a given hash in the URL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM