Chrome扩展程序注入Javascript(嵌入Botpress聊天)

时间:2018-10-09 04:08:00

标签: javascript google-chrome-extension chatbot

我似乎无法将Botpress聊天嵌入我的chrome扩展程序中。这是有关嵌入的botpress文档-https://botpress.io/docs/latest/recipes/embedding/

基本上是2行html脚本的东西。但是在Chrome扩展程序开发人员中,必须注入javascript。所以我就是这样-

main.js(内容脚本)

function injectChat() {
        const script = document.createElement('script');
        script.setAttribute("type","text/javascript");
        script.setAttribute("src", 'https://cb7yghyfdfc.ngrok.io/api/botpress-platform-webchat/inject.js');
        document.body.appendChild(script);

        var settings = document.createElement('script');
        var settingsText = document.createTextNode("window.botpressWebChat.init({ host: 'https://cb7yghyfdfc.ngrok.io' });");
        settings.appendChild(settingsText);
        document.body.appendChild(settings);

        console.log("injecting chat!");
}

manifest.json

{
  "manifest_version": 2,

  "name": "Name",
  "description": "Description",
  "version": "1.0",

  "browser_action": {
    "default_icon": "src/img/logo.png"
  },

  "background": {
        "scripts": ["src/js/background.js"],
        "persistent": false
  },

  "permissions": [
      "https://cb7yghyfdfc.ngrok.io/",
      "https://cb7yghyfdfc.ngrok.io/api/botpress-platform-webchat/inject.js",
      "identity",
      "identity.email",
      "storage"
  ],

  "content_security_policy": "script-src 'self' https://cb7yghyfdfc.ngrok.io/; object-src 'self'",

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["src/build/main.js"],
      "css": ["src/build/main.css"]
    }
  ],

  "web_accessible_resources": [
        "src/build/*"
    ]
}

不幸的是,它没有嵌入,并且出现了此错误-

TypeError: Cannot read property 'init' of undefined
    at <anonymous>:1:24

此初始化来自window.botpressWebChat.init(.....)

0 个答案:

没有答案