@MarcRochkind我想一些知识添加到您的书在Chrome应用集成SQL.js。
这很可能只需很少的努力(考虑到政策和规则的服从)。
为了任何使用eval
整合,需要沙箱脚本的特定部分。在SQL.js的情况下,它是整个库。
这可以通过在主要.html
文档中需要设置的iframe来完成,该文档被称为创建(或主)窗口,例如, chrome.app.window.create('index-app.html', { ..
主文档和iframe之间的通信基地将使用postMessage发送和接收消息。
比方说,这个iframe的来源被称为/iframes/sqljs-sandboxed.html
。 在manifest.json
中,您需要指定sqljs-sandboxed.html
作为沙箱。指定的沙箱可以运行eval
和eval
样构建,如new Function
。
{
"manifest_version": 1,
"name": "SQL.js Test",
..
"sandbox": {
"pages": [
"iframes/sqljs-sandboxed.html",
]
}
}
的sqljs-sandboxed.html
使用一个事件监听器上message
类型的事件作出反应。在这里,您可以简单地添加逻辑(为了简单起见,我使用了switch语句)来执行任何使用SQL.js构造的任何内容。
的sqljs-sandboxed.html
内容为例:
<script src="/vendor/kripken/sql.js"></script>
<script>
(function(window, undefined) {
// a test database
var db = new SQL.Database();
// create a table with some test values
sqlstr = "CREATE TABLE hello (a int, b char);";
sqlstr += "INSERT INTO hello VALUES (0, 'hello');";
sqlstr += "INSERT INTO hello VALUES (1, 'world');";
// run the query without returning anything
db.run(sqlstr);
// our event listener for message
window.addEventListener('message', function(event) {
var params = event.data.params,
data = event.data.data,
context = {};
try {
switch(params.cmd) {
case '/do/hello':
// process anything with sql.js
var result = db.exec("SELECT * FROM hello");
// set the response context
context = {
message: '/do/hello',
hash: params.hash,
response: result
};
// send a response to the source (parent document)
event.source.postMessage(context, event.origin);
// for simplicity, resend a response to see if event in
// 'index-app.html' gets triggered a second time (which it
// shouldn't)
setTimeout(function() {
event.source.postMessage(context, event.origin);
}, '1000');
break;
}
} catch(err) {
console.log(err);
}
});
})(window);
</script>
测试数据库中创建只有一次,事件侦听器镜使用一个简单的开关的API。这意味着为了使用SQL.js,您需要针对API编写代码。这可能乍一看有点不舒服,但从本质上讲,这个想法在实施REST服务时是等同的,在我看来,这从长远来看非常舒适。
为了发送请求,index-app.html
是发起者。需要指出的是,可以异步地向iframe 发出多个请求。为了防止交叉,每个请求都以唯一标识符的形式发送一个状态参数(在我的例子中是unique-ish)。同时在message
事件中附加了一个监听器,该事件筛选出所需的响应并触发其指定的回调,如果触发,则将其从事件堆栈中移除。
对于快速演示,会创建一个对象,用于自动附加和拆除message
事件。最终,listen
函数最终应该过滤特定的字符串值,例如, sandbox === 'sql.js'
(在此示例中未实现),以便加速使用多个沙箱化iframe(例如,用于模板化的handlebars.js)时可能发生的多个message
事件的筛选器选择。
var sqlRequest = function(request, data, callback) {
// generate unique message id
var hash = Math.random().toString(36).substr(2),
// you can id the iframe as wished
content_window = document.getElementById('sqljs-sandbox').contentWindow,
listen = function(event) {
// attach data to the callback to be used later
this.data = event.data;
// filter the correct response
if(hash === this.data.hash) {
// remove listener
window.removeEventListener('message', listen, false);
// execute callback
callback.call(this);
}
};
// add listener
window.addEventListener('message', listen, false);
// post a request to the sqljs iframe
content_window.postMessage({
params: {
cmd: request,
hash: hash
},
data: data
}, '*');
};
// wait for readiness to catch the iframes element
document.addEventListener('DOMContentLoaded', function() {
// faking sqljs-sandboxed.html to be ready with a timeout
setTimeout(function() {
new sqlRequest('/do/hello', {
allthedata: 'you need to pass'
}, function() {
console.log('response from sql.js');
console.log(this.data);
});
}, '1000');
});
为了简单起见,我使用超时来防止在加载iframe之前发送请求。根据我的经验,最好让iframe发送一条消息给iframe加载的父文档,从这里开始,您可以开始使用SQL.js.
最后,在index-app.html
您指定的iframe
<iframe src="/iframes/sqljs-sandboxed.html" id="sqljs-sandbox"></iframe>
凡index-app.html
的内容可以是
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<iframe src="/iframes/sqljs-sandboxed.html" id="sqljs-sandbox"></iframe>
<h1>Hello, let's code with SQL.js!</h1>
<script src="/assets/js/sqljs-request.js"></script>
</body>
</html>
虽然现在我已经切换到node-webkit,因为它扩大了使用节点模块的更多选项的访问权限,但仍然提供的信息看起来非常合理,并且肯定会尝试上述解决方案! – Ric 2015-05-12 09:47:18