简体   繁体   English

Google Chrome扩展程序和Twilio客户端API

[英]Google Chrome extension and Twilio Client API

I'm a newb trying to make a Chrome Extension that utilizes the Twilio Client API and a Node.js back end to make outgoing calls from the browser. 我是一个想要制作Chrome扩展程序的新手 ,它使用Twilio Client API和Node.js后端来从浏览器拨打电话。

I'm having some trouble to run Twilio from my Extension, I get "Twilio is not defined" . 我从扩展程序中运行Twilio时遇到了一些麻烦,我得到"Twilio is not defined"

Here is my manifest file: 这是我的清单文件:

{
  "name": "<NAME>",
  "version": "0.0.1",
  "manifest_version": 2,
  "permissions": [
    "contextMenus",
    "http://localhost:3000/",
    "http://*.twilio.com/*",
    "https://*.twilio.com/*"
  ],
  "background": {
    "scripts": ["lib/jquery-1.7.2.min.js","lib/twilio.js","background.js"]
  }, 
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*", "file:///*/*"],
      "css": ["css/styles.css"],
      "js": ["lib/jquery-1.7.2.min.js"]
    }
  ],
  "icons":{
    "128":"icon_128.png"
  }
}

and here is my background.js: 这是我的background.js:

console.log('Init background.js...'); 

function callNumber(info, tab) {
    alert(info.selectionText);
}

chrome.contextMenus.create ({
    "title":"%s", 
    "contexts": ["all"], 
    "onclick": callNumber
});

// get capability token
$(function() {
    $.get('http://localhost:3000/token', function(resp){
        initTwilio(resp);
    }); 
});

function initTwilio(token) {
    // init twilio
    Twilio.Device.setup(token);
}

Any suggestions on how I can utilize the Twilio Client API? 有关如何使用Twilio Client API的任何建议?

Thanks! 谢谢!

The Twilio script expects to be loaded from the Twilio server. Twilio脚本希望从Twilio服务器加载。 It relies on that to find the rest of the library. 它依赖于它来找到库的其余部分。 To make it happy, you can try the following: 为了让它快乐,您可以尝试以下方法:

Drop the current background section of your manifest and replace it with these lines: 删除清单的当前background部分,并将其替换为以下行:

"content_security_policy": "script-src 'self' https://static.twilio.com; object-src 'self'",
"background": { "page": "background.html" },

And add to your extension a file named background.html with the following content: 并在扩展名中添加名为background.html的文件,其中包含以下内容:

<script src="lib/jquery-1.7.2.min.js"><script>
<script src="https://static.twilio.com/libs/twiliojs/1.1/twilio.min.js"><script>
<script src="background.js"><script>

UPDATE UPDATE

This will fail because the loader attempts to use a URL starting with // , which won't work as expected in a Chrome Extension page. 这将失败,因为加载程序尝试使用以//开头的URL,这在Chrome扩展页面中无法正常工作。 So the easier fix is: 所以更容易解决的问题是:

  1. Go back to your original setting 返回原始设置
  2. Replace the file lib/twilio.js with the contents of http://static.twilio.com/libs/twiliojs/refs/7ed9035/twilio.min.js 将文件lib/twilio.js替换为http://static.twilio.com/libs/twiliojs/refs/7ed9035/twilio.min.js的内容

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

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