我正在编写一个 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 都抛出相同的错误,我不知道为什么。
最佳答案
我认为该语句需要一个字符串,您正在向它传递一个对象,特别是您正在向它传递 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。
关于javascript - Firefox 扩展中的 SQLite3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987859/