【发布时间】:2018-03-23 22:16:20
【问题描述】:
我有一个应用程序使用 background.js 从 Google 表格中提取数据,但我无法在 popup.js 本身中使用该信息。我对 Chrome 应用程序不太熟悉,在这种情况下我该怎么办?如何获取 Chrome 身份或 API 以将信息传递给 popup.js?我无法在 popup.js 中实现 API 调用,因为我需要它在后台工作。
所以我想用 Google Sheets API 修改一张表格,然后读出它。
我基本上是在background.js中做这个的:
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
var response;
var requestURL = "https://sheets.googleapis.com/v4/spreadsheets/" + spreadsheetId + "/values/" + range;
var objectHTTP;
objectHTTP = new XMLHttpRequest();
objectHTTP.onreadystatechange = function() {
if (objectHTTP.readyState == 4 && objectHTTP.status == 200) {
response = objectHTTP.responseText;
}
};
objectHTTP.open("GET", requestURL, false);
objectHTTP.setRequestHeader('Authorization', 'Bearer ' + token);
objectHTTP.send();
// This is the part where I obtain the response, but how do I use it in the main app (popup.js)? Can I somehow share the variables?
});
【问题讨论】:
-
您应该使用Message Passing,扩展程序及其内容脚本之间的通信通过使用消息传递进行。您还可以查看这两个相关的 SO 帖子 1 和 2,看看这是否适合您想要实现的目标。
标签: javascript google-chrome-extension google-chrome-app google-sheets-api