CEF3研究(四)之javascript整合

來源:互聯網
上載者:User

標籤:except   內容   new   tor   nbsp   cep   dom   初始化   value   

一、介紹

Google瀏覽器和CEF使用V8JavaScript Engine作為內容的JavaScript實現。在瀏覽器中的每個視窗都有它自己在的JS上下文提供範圍和在視窗中安全的執行JS代碼。CEF暴露大量JS功能整合在用戶端應用程式。
CEF3的Webkit和JS在單獨的渲染進程中運行。在渲染進程的主線程中使用TID_RENDERER 作為唯一標識。所有V8的執行必須放置在這個線程中。
與JS執行相關的回呼函數被暴露是通過CefRenderProcessHandler介面實現。當一個新的渲染進程被初始化時通過CefApp::GetRenderProcessHandler()函數擷取這個介面。

二、執行JavaScript

在用戶端應用程式中簡單的方式執行JS使用CefFrame::ExecuteJavaScript()函數,這個函數在瀏覽進程一和渲染進程中都可用。在一個JS上下文外可安全使用。

CefRefPtr<CefBrowser> browser = ...;
CefRefPtr<CefFrame> frame = browser->GetMainFrame(); 
frame->ExecuteJavaScript("alert(‘ExecuteJavaScript works!‘);", frame->GetURL(), 0);

上面的引起alert(‘ExecuteJavaScript works!‘)在瀏覽器的主視窗中執行。

在視窗的JS上下文中ExecuteJavaScript函數可用於函數和變數互動。是為了從JS到用戶端應用程式傳回值使用視窗綁定和擴充。

 

三、視窗綁定

 視窗綁定允許用戶端應用程式繫上一個值到視窗的window對象上,視窗綁定的實現使用CefRenderProcessHandler::OnContextCreated()函數。

如:

void MyRenderProcessHandler::OnContextCreated(    CefRefPtr<CefBrowser> browser,    CefRefPtr<CefFrame> frame,    CefRefPtr<CefV8Context> context) {  // Retrieve the context‘s window object.  CefRefPtr<CefV8Value> object = context->GetGlobal();  // Create a new V8 string value. See the "Basic JS Types" section below.  CefRefPtr<CefV8Value> str = CefV8Value::CreateString("My Value!");  // Add the string to the window object as "window.myval". See the "JS Objects" section below.  object->SetValue("myval", str, V8_PROPERTY_ATTRIBUTE_NONE);}

JavaScript架構可以與之互動的視窗綁定。

<script language="JavaScript">alert(window.myval); // Shows an alert box with "My Value!"</script>

視窗綁定是每次重新載入一個架構載入給用戶端應用程式在必要時更改綁定的機會。例如,不同的架構通過修改綁定架構的視窗對象值可以訪問不同的特性。

四、擴充

擴充像window綁定一樣除了為每個架構 載入到上下文之外,一旦載入就不能修改,當一個擴充已經載入並試圖在擴充載入中訪問DOM就會出現DOM不存在的crash。擴充應該在CefRenderProcessHandler::OnWebKitInitialized()函數中使用CefRegisterExtension函數註冊。

void MyRenderProcessHandler::OnWebKitInitialized() {  // Define the extension contents.  std::string extensionCode =    "var test;"    "if (!test)"    " test = {};"    "(function() {"    " test.myval = ‘My Value!‘;"    "})();";  // Register the extension.  CefRegisterExtension("v8/test", extensionCode, NULL);}

通過extensionCode描述的字串可以是任何有效代碼,JS架構可以和擴充代碼進行互動

<script language="JavaScript">alert(test.myval); // Shows an alert box with "My Value!"</script>

五、基本JS類型

CEF支援基礎資料型別 (Elementary Data Type)的建立,包括:undefined, null, bool, int, double, date 和 string.這些基礎資料型別 (Elementary Data Type)使用CefV8Value::Create*()系列靜態函數建立。如建立一個JS字串:CefRefPtr<CefV8Value> str = CefV8Value::CreateString("My Value!");

基礎資料型別 (Elementary Data Type)可在任何地方建立和在所關聯的上下文中不用初始化。如:

CefRefPtr<CefV8Value> val = ...;
if (val.IsString())
{ // The value is a string. }

使用Get*Value()系列函數擷取值:CefString strVal = val.GetStringValue();

六、JS數組

使用CefV8Value::CreateArray()靜態函數並傳遞一個長度作為參數建立數組。數組只能在上下文內部建立並使用。如:

CefRefPtr<CefV8Value> arr = CefV8Value::CreateArray(2);

 值賦給一個數組使用SetValue()方法的變體,以一個索引作為第一個參數。
arr->SetValue(0, CefV8Value::CreateString("My First String!"));
arr->SetValue(1, CefV8Value::CreateString("My Second String!"));

IsArray()函數測試CefV8Value是否為數組,GetArrayLength()函數擷取資料的長度,從數組中擷取一個值使用GetValue()變體函數。

七、JS對象

使用CefV8Value::CreateObject靜態函數可帶一個可選的CefV8Accessor參數建立JS對象。對象也只能在js上下文中建立並使用。

 CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(NULL);

使用SetValue()變體函數並以字串key作為第一參數給對象分配值。

八、對象的訪問者

JS對象可選擇使用一個與之關聯的CefV8Accessor以提供一個源生的getting和setting值的實現。
CefRefPtr<CefV8Accessor> accessor = …;
CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(accessor);

CefV8Accessor介面的一個實現,必須由用戶端應用程式提供。

class MyV8Accessor : public CefV8Accessor {public:  MyV8Accessor() {}  virtual bool Get(const CefString& name,                   const CefRefPtr<CefV8Value> object,                   CefRefPtr<CefV8Value>& retval,                   CefString& exception) OVERRIDE {    if (name == "myval") {      // Return the value.      retval = CefV8Value::CreateString(myval_);      return true;    }    // Value does not exist.    return false;  }  virtual bool Set(const CefString& name,                   const CefRefPtr<CefV8Value> object,                   const CefRefPtr<CefV8Value> value,                   CefString& exception) OVERRIDE {    if (name == "myval") {      if (value.IsString()) {        // Store the value.        myval_ = value.GetStringValue();      } else {        // Throw an exception.        exception = "Invalid value type";      }      return true;    }    // Value does not exist.    return false;  }  // Variable used for storing the value.  CefString myval_;  // Provide the reference counting implementation for this class.  IMPLEMENT_REFCOUNTING(MyV8Accessor);};

為了將值傳遞給訪問者必須使用SetValue()變體函數設定,接受AccessControl和PropertyAttribute參數

九、JS函數

CEF支援JS函數建立和本地實現,

 

CEF3研究(四)之javascript整合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.