代码之家  ›  专栏  ›  技术社区  ›  Marl

Firebreath插件:如何在c++部件中获取文档财产?

  •  0
  • Marl  · 技术社区  · 12 年前

    如何在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 。我不知道该使用什么辅助方法。

    2 回复  |  直到 12 年前
        1
  •  2
  •   taxilian    12 年前

    对不起,你在FireBreath聊天室问我的时候我正在睡觉。一个略为简短的方法是:

    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
    }
    

    您应该始终在try中包装convert_cast。。catch,以防转换失败。这里的DOM::Document对象上的getProperty抽象基本上只是在内部执行convert_cast。

        2
  •  0
  •   Marl    12 年前

    如果有人想知道答案:

    FB::DOM::DocumentPtr dom = m_host->getDOMDocument();
    if (dom && dom->getJSObject()->HasProperty("title")) {
        std::string title = m_host->getDOMDocument()->getJSObject()->GetProperty("title").convert_cast<std::string>();
    }