Firefox扩展中的SQLite3错误

时间:2015-06-22 19:06:30

标签: javascript sqlite firefox-addon

我正在编写一个Firefox扩展程序,它将获取当前浏览器的句柄并将其写入sqlite数据库。我的整个代码是:

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");

let file = FileUtils.getFile("ProfD", ["testing.sqlite"]);
let dbConn = Services.storage.openDatabase(file);

var browserWindow = Services.wm.getMostRecentWindow('navigator:browser');
if (!browserWindow) {
    throw new Error('No browser window found');
}

var baseWindow = browserWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIWebNavigation)
                              .QueryInterface(Ci.nsIDocShellTreeItem)
                              .treeOwner
                              .QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIBaseWindow);

var hwndString = baseWindow.nativeHandle;

Components.utils.import('resource://gre/modules/ctypes.jsm');

var user32 = ctypes.open('user32.dll');

/* http://msdn.microsoft.com/en-us/library/ms633539%28v=vs.85%29.aspx
 * BOOL WINAPI SetForegroundWindow(
 *   __in_ HWND hWnd
 * );
 */
var SetForegroundWindow = user32.declare('SetForegroundWindow', ctypes.winapi_abi,
    ctypes.bool, // return BOOL
    ctypes.voidptr_t // HWND
);

var hwnd 
hwnd = ctypes.voidptr_t(ctypes.UInt64(hwndString));
var rez_SetForegroundWindow = SetForegroundWindow(hwnd);

console.log('hwnd: ', hwnd.toString());
dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwnd +")");
//dbConn.executeSimpleSQL("INSERT INTO tblHandles(handle) VALUES("+ hwnd+")");

user32.close();

我收到错误:

NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]

在这一行:

dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwnd +")");

createStatement和executeSimpleSQL都抛出同样的错误,我不知道为什么。

1 个答案:

答案 0 :(得分:1)

我认为该语句需要一个字符串,你将它传递给一个对象,特别是你传递它CData { }这就是行hwnd = ctypes.voidptr_t(ctypes.UInt64(hwndString));所做的。因此,而不是dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwnd +")");执行此操作:

dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwndString +")");

同样jsctypes很酷,但我认为最好只在绝对必要的情况下使用,因为我认为性能稍差。所以不需要使用它来聚焦窗口。而只是做browserWindow..focus();

注意: 另外我看到你将Components.utils和Ci混合在一起定义Ci是什么?如果你不是为什么不在那里定义Cu并且做Cu而不是Components.utils。