Que*_*ura 8 javascript google-calendar-api google-chrome-extension google-api-js-client
我现在正在花几个小时搜索如何在 Chrome 扩展中使用 Google API。我想做的就是解析网站的内容并将其作为新事件插入到 Google 日历中。我得到了解析和一切,但似乎不可能在 Chrome 扩展中使用 Google API。我只是想在单击 chrome 扩展中的按钮时编写一个示例事件,但它一直拒绝加载 Google API,并出现以下错误:
Refused to load the script 'https://apis.google.com/js/platform.js' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
Run Code Online (Sandbox Code Playgroud)
我的清单.json:
{
"manifest_version": 2,
"name": "DVB2Calender",
"description": "This extension will export the current viewed schedule to your Google Calender.",
"version": "1.0",
"content_security_policy": "script-src 'self' https://apis.google.com/; object-src 'self'",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab"
]
}
Run Code Online (Sandbox Code Playgroud)
我的 popup.html
Refused to load the script 'https://apis.google.com/js/platform.js' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
Run Code Online (Sandbox Code Playgroud)
我的 popup.js:
document.addEventListener('DOMContentLoaded', function() {
var checkPageButton = document.getElementById('exportToCalender');
checkPageButton.addEventListener('click', function() {
chrome.tabs.getSelected(null, function(tab) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://apis.google.com/js/client.js?onload=callbackFunction";
head.appendChild(script);
d = document;
var download = d.getElementsByClassName('icon-link ico-calender')[6];
var request = makeHttpObject();
request.open("GET", download, true);
request.send(null);
request.onreadystatechange = function() {
if (request.readyState === 4) {
var resultText = request.responseText;
array = CSVToArray(resultText, ":");
alert(resultText);
var resource = {
"summary": "Appointment",
"location": "Somewhere",
"start": {
"dateTime": "2011-12-16T10:00:00.000-07:00"
},
"end": {
"dateTime": "2011-12-16T10:25:00.000-07:00"
}
};
var request = gapi.client.calendar.events.insert({
'calendarId': 'primary',
'resource': resource
});
request.execute(function(resp) {
console.log(resp);
});
}
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 1
看起来您只是忘记重新加载扩展包。请注意,每次对扩展代码进行更改时,都chrome://extensions/ 需要重新加载(可以在此处完成)以使代码更改生效。
要检查客户端脚本是否可以使用,建议添加 onload 回调,如下所示:
window.callbackFunction = function(){
alert('client.js ready')
// here goes google client api calls
}
Run Code Online (Sandbox Code Playgroud)
onload回调函数的名称由脚本url中的参数指定onload。