chrome extension - manifest version 2

痞子三分冷 提交于 2019-12-13 17:55:49

问题


I have a chrome extension that has a reference to the jquery file.
this is my popup html (only the head tag):

<head>
    <title>My Extention</title>
    <script type="text/javascript" src="http://www.MySite.com/Resources/JS/JQuery/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="MyExtensionScript.js"></script>
</head>

so in "MyExtensionScript.js" i thought i could use jquery but apparently the $ function is not defined.
This is my manifest.json file:

{
  "name": "My Test Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Test version of My Extension",
  "browser_action": {
    "default_icon": "test.ico",
    "default_popup": "Test.html"
  },
  "permissions": [
    "cookies",
    "tabs",
    "<all_urls>"
  ]
}

in version 1 of the manifest it worked, but now it doesn't. I tried to use the "web_accessible_resources" and add to them "http://www.MySite.com/Resources/JS/JQuery/jquery-1.7.2.min.js" but that didn't work also. any ideas?
also, i have a script injected to the current page and returning me a message (in my case some html source of the current page), will this behavior be affected by the transition to manifest version 2? Thanks all :)

EDIT: I have a web application that enables cross domain scripting (using JSONP). In my extension i had a script calling a web service in my site with $.getJSON. now it doesn't work. i'm pretty sure that it has to do with the new manifest version but how can i enable again the cross domain scripting?


回答1:


You need to use a jQuery file stored locally in your extension, rather than referenced from your site.

This is due to Chrome's strict Content Security Policy that only allows local scripts to be executed, with no inline code.

Web Accessible Resources are files inside your extension that may be accessed from the web, rather that resources your extension can access that are on the web. For example, if you wanted to change the background image of a page using an image stored in the extension, you have to add that image to the list in web_accessible_resouces in your manifest.

The change of manifest version should not affect your content scripts, unless they do something that is no longer allowed. You can see what else has changed from the Chrome manifestVersion docs.




回答2:


I just include jquery in my content scripts. just make sure to load it before your script.

{
  "manifest_version": 2,
  "default_title": "Babble",
  "version": "1.2",
  "description": "Chat in your language with friends in their language",
  "default_locale": "en",
  "default_icons": {
    "16": "img/icon16.png",
    "48": "img/icon48.png",
    "128": "img/icon128.png"
  },

  "content_scripts":[
    {
        "matches": ["http://mail.google.com/*", "https://mail.google.com/*"],
        "css" : ["css/style.css"],
        "js" : ["js/jquery.js" , "js/translate.js" , "js/jquery.cookie.js"]
    }
  ]
}


来源:https://stackoverflow.com/questions/11814270/chrome-extension-manifest-version-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!