如何在c++部分获取文档属性?例如,我想获取 document.title 并将其存储在 firebreath 插件的 c++ 部分?
if (window && window->getJSObject()->HasProperty("domain")) {
FB::JSObjectPtr docObj = window->getProperty<FB::JSObjectPtr>("document");
consoleObj->Invoke("log", FB::variant_list_of("Has obtained document"));
if(docObj && docObj->HasProperty("domain")){
m_domain = docObj->getJSObject()->getProperty<std::string>("domain");
consoleObj->Invoke("log", FB::variant_list_of("Has obtained domain: " + m_domain));
}
}
但是这个编译失败,因为 docObj 没有方法 HasProperty
。我不知道要使用什么辅助方法。
最佳答案
抱歉,当你在 FireBreath 聊天室提问时我正在 sleep 。一个稍微更简短的方法是:
FB::DOM::DocumentPtr dom = m_host->getDOMDocument();
try {
if (dom && dom->getJSObject()->HasProperty("title")) {
std::string title = m_host->getDOMDocument()->getProperty<std::string>("title");
}
} catch (...) {
// Could not get the title
}
您应该始终将 convert_cast 包装在 try .. catch 中,以防转换失败。此处 DOM::Document 对象的 getProperty 抽象基本上只是在内部执行 convert_cast。
关于c++ - Firebreath 插件 : How to get document properties in the c++ part?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19289860/