我正在制作一个非常简单的扩展程序来随机更改我的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()'中咳嗽了回调,但那根本不是。 –