Chrome 插件拉取服务端数据

发布时间: 2018-02-20 13:49:35 作者: 大象笔记

manifest.json 中添加可信域名

"permissions": [
    "http://www.sunzhongwei.com/"
],

事件逻辑中

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.sunzhongwei.com/some_api", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    document.getElementById("resp").innerHTML = xhr.responseText;
    ...
  }
}
xhr.send();

如果想使用 jquery 的 ajax 只需要在插件的 html 中引入 jquery 即可。

参考

https://developer.chrome.com/extensions/xhr

我是一名山东烟台的开发者,联系作者