简体   繁体   English

我正在尝试让jQuery在我的chrome扩展程序的background.js中工作,并且它将无法工作。 为什么?

[英]I'm trying to get jquery working in the background.js of my chrome extension and it won't work. why?

I've read up on quite a few stack overflow questions about jQuery and background pages and simply do not understand the rules of engagement. 我已经阅读了很多有关jQuery和背景页面的堆栈溢出问题,但是根本不了解参与规则。 I don't know how they interact. 我不知道他们是如何互动的。 Can somebody tell me why this code does not work? 有人可以告诉我为什么此代码不起作用吗?

manifest.json

{
  "name": "test 1",
  "description": "testing jquery",
  "version": "2.0",
  "permissions": [
    "activeTab"
  ],
  "background": {
    "scripts": [ "jquery-2.1.1.js", "background.js"],
    "persistent": false
  },
  "browser_action": {
     "default_title": "test"
  },
  "manifest_version": 2
}

i don't forget to include the jQuery file when i upload the extension 上传扩展名时,我不会忘记包含jQuery文件

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(
    { code: 'console.log("this works");'},
    { code: '$( document ).ready(function() { console.log( "but this does not work :(" ); });' }
  );
});

keep in mind i'm familiar with how to use jQuery using content scripts but the problem with these is then the 请记住,我熟悉如何使用内容脚本使用jQuery,但是这些的问题是

chrome.****** 

commands don't work. 命令不起作用。

Please help! 请帮忙!

You are correctly loading jQuery into the background page. 您正在将jQuery正确加载到后台页面中。 If you were to use $(document).ready() in its code, it would work. 如果要在其代码中使用$(document).ready() ,它将可以正常工作。

The problem is, when you use executeScript , the code you specify does not execute in the same context as background.js . 问题是,当您使用executeScript ,您指定的代码不会在与background.js相同的上下文中执行。 A brand new JS context is created, attached to the tab in question, and that context does not have jQuery in it (even if the tab's context itself has it, by the way, see isolated context ). 创建了一个全新的JS上下文,并将其附加到有问题的选项卡上,并且该上下文中没有jQuery(即使该选项卡的上下文本身具有jQuery,请参见隔离的上下文 )。

Furthermore, you're calling executeScript wrong. 此外,您在调用executeScript错误。 Its arguments are: 它的参数是:

  • integer (optional) tabId 整数(可选) tabId
  • object details 对象details
  • function (optional) callback 函数(可选) callback

You are calling it with two objects; 您正在用两个对象调用它; it doesn't work like that. 它不是那样工作的。


So, to fix, you will need to inject jQuery first in that execution context, and then your code. 因此,要解决此问题,您需要先在该执行上下文中注入jQuery,然后再注入代码。 You need to chain 2 calls to ensure they execute in the correct order. 您需要链接2个调用以确保它们以正确的顺序执行。

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(
    tab.id,
    { file: 'jquery-2.1.1.js' },
    function() {
      chrome.tabs.executeScript(
        tab.id,
        { code: '$( document ).ready(function() { console.log( "This works now" ); });' }
      );
    }
  );
});

暂无
暂无

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

相关问题 我的“background.js”代码用于获取选定的 chrome 标签 url 在我的 chrome 扩展中无法正常工作 - My “background.js” code to get the selected chrome tabs url is not working properly in my chrome extension 为什么我的 content.js 脚本不能与我的 background.js 脚本(chrome 扩展)通信? - Why won't my content.js script communicate with my background.js script (chrome extensions)? Background.js无法使用Chrome扩展程序 - Background.js not working Chrome Extension 我正在尝试按照老师的指示使用 img.src 在 JS 上获取一些照片,但代码不起作用。 我的代码看起来完全一样 - I'm trying to get some photos on JS using img.src as my teatcher instructed but the code won't work. My code looks exactly the same tho Chrome扩展程序:尝试获取选项卡URL时,Background.js“未捕获的TypeError” - Chrome Extension: Background.js “Uncaught TypeError” when trying to get tab url Chrome扩展程序background.js仅在用户为cookie时可用 - Chrome Extension background.js only to work if user is cookie is available 如何在Google Chrome扩展程序中将信息从Background.js传递到autofill.js? - How can I pass information from Background.js to autofill.js in my Google Chrome extension? 从Chrome扩展程序的background.js获取响应 - Get Response From background.js of chrome extension 我正在尝试做一个chrome查找和替换扩展程序,但是它不起作用 - I'm trying to make a chrome find-and-replace extension, but it won't work 在 background.js 中的 Chrome 扩展中的 setInterval - setInterval in Chrome extension in background.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM