简体   繁体   English

Chrome Extension Manifest权限

[英]Chrome Extension Manifest permissions

This is my first chrome Extension. 这是我的第一个Chrome扩展程序。

I basically have already written a html file that has a javascript file to go along with it. 我基本上已经写了一个带有javascript文件的html文件。 I've noticed that apparently you have to put the javascript as a separate file to the html file. 我注意到,显然您必须将javascript作为单独的文件放置到html文件中。 That's fine. 没关系。 Did that. 做过某事。 Referenced it in the html header. 在html标头中引用了它。

But I'm not sure how to setup the manifest file to be able to use the javascript on my popup html page. 但是我不确定如何设置清单文件以能够使用弹出html页面上的javascript。

the javascript does not use jquery. javascript不使用jquery。 it's pure javascript. 这是纯JavaScript。

I'm not sure what Required or Optional Permissions I have to include in the manifest if any. 我不确定清单中必须包含哪些必需或可选权限。 So far all I know is that I might have to include "clipboardRead" permission in there. 到目前为止,我所知道的是我可能必须在其中包含“ clipboardRead”权限。

Or am I meant to be using content_scripts instead in the manifest. 还是我打算在清单中使用content_scripts。

this is my manifest. 这是我的清单。

{
  "manifest_version": 2,

  "name": "PhotoTagger",
  "description": "This extension helps you create your hashtags for your posts, quickly.",
  "version": "1.0",

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["PhotoTagger.js"]
    }
  ],

  "permissions": [
    "clipboardRead",
  ],

  "browser_action": {
    "default_icon": "icons/Google-plus-19x19.png",
    "default_popup": "PhotoTagger.html"
  }
}

and a sample function in my javascript is this (PhotoTagger.js): 我的JavaScript中的示例函数是这样的(PhotoTagger.js):

function selectText(containerid) {
    if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(document.getElementById(containerid));
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(document.getElementById(containerid));
        window.getSelection().addRange(range);
    }
}

and this is a sample snippet of my html calling the Js function: 这是我的html调用Js函数的示例代码段:

<li id="#Sydney" onclick="selectText('#Sydney')">
    <input id="al2" type="checkbox" value="#Sydney +104262770460851770269"/>
    #Sydney</li>

really appreciate any help I can get. 非常感谢我能得到的任何帮助。

Also to mention the html works fine on an opened tab in chrome. 另外要提一下,html在chrome中打开的选项卡上工作正常。 just doesn't work when I run it as an extension. 当我将其作为扩展运行时不起作用。

I've just begun using ChromeExtensions, so this might not be a complete answer, but here are some issues I see: 我刚刚开始使用ChromeExtensions,所以这可能不是一个完整的答案,但是我看到了一些问题:

  1. content_scripts are injected into the page and have access to the webpage's dom, but not it's JS content_scripts被注入到页面中并可以访问网页的dom,但不是JS
  2. popups live in their own sandbox and have access only to what's in that sandbox 弹出窗口位于其自己的沙箱中,并且只能访问该沙箱中的内容
  3. webpages cannot access JS in either the content_scripts or the popups 网页无法在content_scripts或弹出窗口中访问JS

PhotoTagger.html should be able to interact with its own JS, just by putting in a "script" tag referencing PhotoTagger.js. 仅通过放入引用PhotoTagger.js的“脚本”标签,PhotoTagger.html就应该能够与其自己的JS进行交互。 There are no permissions needed for that. 不需要任何权限。 So if your snippet is in PhotoTagger.html and there's a "script" reference to PhotoTagger.js in PhotoTagger.html, then you should be fine (and you can get rid of the whole content_scripts section). 因此,如果您的代码段位于PhotoTagger.html中,并且在PhotoTagger.html中有一个“脚本”对PhotoTagger.js的引用,那么您应该没事(您可以摆脱整个content_scripts部分)。 If you're doing this and it doesn't work, you might need to link more of the code. 如果您这样做却行不通,则可能需要链接更多代码。

However, if you're loading a webpage with that html snippet on it, it won't work. 但是,如果您正在加载带有该html代码段的网页,则它将无法正常工作。 The webpage does not have access to JS in the content_scripts or in the popup (I'm still looking for a workaround for this, but it's blocked because of security issues). 该网页无法访问content_scripts或弹出窗口中的JS(我仍在寻找解决方法,但由于安全问题而被阻止)。

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

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