我正在製作一個非常簡單的擴展程序來隨機更改我的chromebook上的壁紙。但是,我得到一個奇怪的錯誤在JavaScript控制檯後,我加載它:高級函數會導致不安全的擴展變量
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". @ background.js:27
這裏是我的manifest.json:
{
"manifest_version": 2,
"name": "Abyss Wallpapers for Chrome OS",
"description": "Sets the Chrome OS wallpaper to a random wallpaper from the
Wallpaper Abyss (wall.alphacoders.com), once a minute",
"version": "1.0",
"background":
{
"scripts":["background.js"]
},
"permissions": [
"wallpaper",
"https://wall.alphacoders.com/api2.0/",
"background"
]
}
這裏是我的background.js:
var baseUrl = "https://wall.alphacoders.com/api2.0/get.php?"
var apiKey = "c160c64ef4c79e61e325ddf944183dfe"
var params = "auth=" + apiKey + "&method=random"
var interval = 30000;
var xhr = new XMLHttpRequest();
xhr.open("GET", baseUrl + params, true);
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
var data = xhr.responseText;
if (data.success)
{
chrome.wallpaper.setWallpaper
(
{url:data.wallpapers[1].url_image,
layout:"CENTER_CROPPED",
filename:data.wallpapers[1].id + "." + data.wallpapers[1].file_type},
function(thumbnail)
{
thumbnail = null;
}
)
}
}
}
setInterval(xhr.send(), interval);
它似乎拒絕了setInterval()
方法,但沒有eval甚至沒有字符串。
編輯:所以我改變了setInterval()
聲明以反映BERGI的答案,現在後臺腳本運行大約一分鐘,這意味着間隔設置,但我得到另一個錯誤後:
Uncaught InvalidStateError: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.(anonymous function) @ background.js:27
爲什麼當我在第5行專門調用xhr.open()
時,對象不會打開?
我不認爲'setInterval'是一個高階函數。 – Bergi
的確如此,我最初認爲Chrome在'onreadystatechange()'中咳嗽了回調,但那根本不是。 –