日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CEF3—在网页加载前给js对象填值

發布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CEF3—在网页加载前给js对象填值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • CEF3—在網頁加載前給js對象填值
    • 前言
    • 思路
    • 代碼

CEF3—在網頁加載前給js對象填值

前言

記錄一次筆者在實際開發中遇到的問題。在用cef做多頁應用開發的時候,多個單頁共享數據的問題。這個數據被前端稱之為token,在多頁應用中需要被共享。數據被使用和存的動作都是在render進程,而多個h5頁面也就是多個 render進程中,但是存放的位置肯定是在browser進程,因為browser進程是后臺進程,大家取數據都到同一個進程中去存取 數據也就被共享。

思路

筆者一開始設計的接口如下:

BB.SetToken(x); // 存token BB.Token(function(x){console.log(x);}); // 取token

存 會去更新token。 取 需要通過回調函數取,這里需要在回調函數中去,因為數據并不在render進程中 只能異步取。看是去一切還很簡單 美好。但是 前端并不想通過回調函數的方式取token,就想通過類似下面的方式去取。

// 前端想快速方便的取到token,類似下面這種方式 var token = BB.Token(); // 方法返回值方式 var token = BB.token; // 對象屬性的方式

當然前端的要求并不過分,就得我們去想辦法去實現了!這里如果用方法的返回值的話,必須使用全局的事件進行等待,等從browser取到了還要從共享內存或者其他方式把token數據取到。所以筆者當然也想用最簡單方式去實現,這里是使用 BB.token對象屬性值的方式。

如果用對象屬性的方式的話,我們 token的填值 就是我們客戶端作為主動方進行填值。如果 有單頁使用SetToken方法更新token,那么此時 客戶端要更新所有單頁上的 BB.token。如果新開一個單頁而且js代碼要立即取到BB.token的值,這里也就是筆者遇到的問題。因為筆者 更新某個單頁的上的BB.token邏輯已寫好,當新單頁打開加載后 直接給新開的單頁 發送消息讓其更新token即可,但是這樣回有一個延時問題 在頁碼加載過程中 js代碼取不到token而要等1s后才能取到。

代碼

所以 不能等單頁加載完后 去發送更新token的消息去進行更新。這里必須是在 創建單頁或者在單頁加載完前 類似window對象的內置屬性一樣 可以直接取到token。網上沒找到參考的,所以筆者去仔細看了一下cef接口 果然找到了。主要用到下面幾個接口。

// 創建單頁會用到下面這個接口,對就是這個函數,仔細看這個函數的extra_info參數,該數據可以將browser進程的數據帶到創建單頁過程中的回調函數中CefRenderProcessHandler::OnBrowserCreated()// CefBrowserHost的靜態成員函數///// Create a new browser window using the window parameters specified by// |windowInfo|. All values will be copied internally and the actual window// will be created on the UI thread. If |request_context| is empty the// global request context will be used. This method can be called on any// browser process thread and will not block. The optional |extra_info|// parameter provides an opportunity to specify extra information specific// to the created browser that will be passed to// CefRenderProcessHandler::OnBrowserCreated() in the render process.////*--cef(optional_param=client,optional_param=url,optional_param=request_context,optional_param=extra_info)--*/ static bool CreateBrowser(const CefWindowInfo& windowInfo,CefRefPtr<CefClient> client,const CefString& url,const CefBrowserSettings& settings,CefRefPtr<CefDictionaryValue> extra_info,CefRefPtr<CefRequestContext> request_context);// 所以就找到CefRenderProcessHandler中相關的回調函數// CefRenderProcessHandler中可被重寫的回調函數// 該函數可以取到從browser進程中帶過來的數據///// Called after a browser has been created. When browsing cross-origin a new// browser will be created before the old browser with the same identifier is// destroyed. |extra_info| is a read-only value originating from// CefBrowserHost::CreateBrowser(), CefBrowserHost::CreateBrowserSync(),// CefLifeSpanHandler::OnBeforePopup() or CefBrowserView::CreateBrowserView().////*--cef()--*/virtual void OnBrowserCreated(CefRefPtr<CefBrowser> browser,CefRefPtr<CefDictionaryValue> extra_info) {}// 下面這個函數注釋寫的很清楚,下面是Chrome V8環境一旦被創建好就會被回調,所以在下面回調函數中取我們的自定義對象或者 window對象一定是可以取到并能賦值的///// Called immediately after the V8 context for a frame has been created. To// retrieve the JavaScript 'window' object use the CefV8Context::GetGlobal()// method. V8 handles can only be accessed from the thread on which they are// created. A task runner for posting tasks on the associated thread can be// retrieved via the CefV8Context::GetTaskRunner() method.////*--cef()--*/virtual void OnContextCreated(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefV8Context> context) {}

關鍵代碼如下:

// browser進程中創建單頁 CefRefPtr<CefDictionaryValue> extra_info = CefDictionaryValue::Create(); extra_info->SetString("token",m_strToken); CefBrowserHost::CreateBrowser(windowInfo, m_browserEvent, strUrl, browserSetting, extra_info, NULL);// render進程中處理數據并給js對象進行屬性賦值 static CefString m_strToken = ""; void CCefBrowserApp::OnBrowserCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDictionaryValue> extra_info) {m_strToken = extra_info->GetString(CefString("token")); }void CCefBrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) {context->Enter();CefRefPtr<CefV8Value> window = context->GetGlobal();CefRefPtr<CefV8Value> BB = window->GetValue(CefString("BB"));if(BB->IsObject()){CefRefPtr<CefV8Value> token = CefV8Value::CreateString(m_strToken);BB->SetValue(_T("token"),token,CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_DONTDELETE); }context->Exit(); }

總結

以上是生活随笔為你收集整理的CEF3—在网页加载前给js对象填值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。