我正在开发简单的 Chrome 打包应用。我是 chrome 应用程序开发的新手。运行应用程序时出现错误。
错误消息是:无法打开指向“chrome-extension://ckikacajammagdadocdkmcoecilafkgl/index.html”的同窗口链接;试试 target="_blank".
索引 HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first Chrome App</title>
<script src="jquery-1.10.2.min.js"></script>
<script src="custom.js"></script>
</head>
<body>
<button id="btnTest" value="Test" style="width: 200px; height: 40px">TEST</button>
<br />
<input type="text" id="txtTest" />
<div id="divMovies" style="width: 100%;">
<br />
Url :
</div>
</body>
</html>
自定义.js:
$(document).ready(function () {
save(load);
});
function save(callback) {
var channel = "Test local storage item test";
chrome.storage.local.set({ 'channel': channel });
callback();
}
function load() {
var channelss = "";
chrome.storage.local.get('channel', function (result) {
channelss = result;
});
document.getElementById("txtTest").value = channelss.channel;
}
list .json:
{
"manifest_version": 2,
"name": "Dizi Takip App",
"version": "1",
"app": {
"background": {
"scripts": ["main.js"]
}
},
"icons": { "128": "icon.png" },
"offline_enabled": true,
"permissions": [
"storage"
]
}
主要.js :
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {width: 500, height: 309});
});
怎么了?
最佳答案
chrome.storage.*.set
和 chrome.storage.*.get
都是异步 API(正如 sowbug 所指出的)。因此,您需要将 custom.js 更改为如下所示:
function save(callback) {
var channel = "Test local storage item test";
chrome.storage.local.set({ 'channel': channel }, function() {
callback();
});
}
function load() {
chrome.storage.local.get('channel', function (result) {
document.getElementById("txtTest").value = result.channel;
});
}
但是,您提到的错误对该应用没有任何意义,并且绝对与 custom.js 中的问题无关。如果您的应用程序在其 HTML 代码中有一个没有 target='_blank' 属性的链接,并且用户点击了该链接,您通常会看到此错误。应用程序没有内置的窗口内导航概念,因此此类链接是被禁止的。您可以为链接指定 target='_blank' 并让它在用户的默认浏览器(不一定是 Chrome!)中打开一个新选项卡/窗口,或者向链接添加一个 onclick 处理程序并在其中做一些更有趣的事情。
我根据您的来源重新创建了您的应用程序,它开箱即用,除了 txtTest 在没有上述更改的情况下说“未定义”。但是任何地方都没有报错(any error)。但是,我没有使用 jQuery。相反,我在 custom.js 中使用以下内容来启动序列:
onload = function () {
save(load);
};
如果您可以验证您在使用 $(document).ready 时收到错误,而在使用 onload 时没有收到错误,请告诉我们。
关于javascript - Chrome 打包应用程序无法打开同一窗口错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18811856/