Method and system for dynamically modifying user agent based on cef framework

By utilizing key event callbacks in the CEF framework, executing JavaScript scripts and modifying HTTP request headers, the problem that UA cannot be modified dynamically in the CEF framework is solved, and flexible UA adjustments are achieved for different browsers in the same program.

CN120085844APending Publication Date: 2025-06-03浙江讯盟科技有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510200008.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-24
Publication Date
2025-06-03

AI Technical Summary

Technical Problem

The existing CEF framework cannot be dynamically modified, affecting the settings of multiple browsers, making it difficult to achieve flexible adjustments in multiple different UA scenarios.

Method used

By using key event callbacks in the CEF framework, especially CefLoadHandler::OnLoadStart and CefResourceRequestHandler::OnBeforeResourceLoad callbacks, executing JavaScript scripts and modifying UA information in the HTTP request header, dynamically modifying UA.

Benefits of technology

It realizes that different browsers use different UA in the same program, meets the needs of changing browser usage environments, and provides flexible modification capabilities for UA information.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120085844A_ABST
    Figure CN120085844A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of browsers, in particular to a method and system for dynamically modifying a user agent based on a cef framework, and the method comprises the following steps: step 1, creating a Web browser control; step 2, a developer sets a UA (Unified Architecture) of a self-defined web browser in the Web browser control; and step 3, the UA information used in the business scene is dynamically modified through the UA of the customized web browser in the step 2, and UA modification is more flexible.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of browsers, and particularly relates to a method and system for dynamically modifying a user agent based on the CEF framework. Background Art

[0002] CEF is an open-source Web Browser control of Google Chromium. Based on this control, developers can develop personalized cross-platform browser software or components. In the browser technology framework, the UA plays an important role. It can describe system information such as the operating system information and browser version information of the current software environment. Generally speaking, in many business scenarios, some web application servers will return different data to the client according to the system information described by the UA.

[0003] The existing technology can set the UA of the browser through a global interface in the code or modify the UA of the web browser through command-line parameters.

[0004] 1. The UA set through the global interface in CEF cannot be dynamically modified. Once set, the subsequent opened browsers can only be the UA set at the beginning.

[0005] 2. There is a problem with setting the UA through the command-line method. In the CEF architecture, the callback function set by the command line is global, and once modified, it will affect all subsequent web browsers; when it is necessary to create multiple web browsers with different UAs at the same time, the global variable method is very difficult to implement. Summary of the Invention

[0006] The purpose of the present invention is to provide a method and system for dynamically modifying a user agent based on the CEF framework with more flexible UA modification.

[0007] The above object of the present invention is achieved through the following technical solutions: A method for dynamically modifying a user agent based on the CEF framework, including the following steps: Step 1, create a Web browser control; Step 2, the developer sets a custom UA for the web browser in the Web browser control; Step 3, dynamically modify the UA information used in the business scenario through the custom UA of the web browser in Step 2.

[0008] As a preference of the present invention, the business scenario includes that the web server performs corresponding business processing according to the UA and the JS file performs corresponding business processing according to the UA.

[0009] Preferably, in the present invention, the modification of the UA information in the business scenario is processed through key event callbacks.

[0010] Preferably, in the present invention, the key event callbacks include the CefLoadHandler::OnLoadStart callback and the CefResourceRequestHandler::OnBeforeResourceLoad callback.

[0011] Preferably, in the present invention, the process of dynamically modifying the UA information used in the business scenario by the Web browser control in the CefLoadHandler::OnLoadStart callback specifically includes: executing a piece of JS script in the web browser to modify the UA property in the navigator object in the JS running environment of the current page, so as to ensure that when the JS script in the subsequent page obtains the UA through the navigator.userAgent method, the value obtained is the UA value custom-set by the developer.

[0012] Preferably, in the present invention, the pseudocode of the process of dynamically modifying the UA information used in the business scenario by the Web browser control in the CefLoadHandler::OnLoadStart callback is as follows: void CCefHandler::OnLoadStart(CefRefPtr <cefbrowser>browser, CefRefPtr <cefframe>frame, TransitionType transition_type) { std::string ua = "customized_user_agent”; std::string change_ua_script = "Object.defineProperty(navigator, 'userAgent', {get: function () { return '" + ua + "';}});"; frame->ExecuteJavaScript(change_ua_script,frame->GetURL(), 0); }。

[0013] As a preference of the present invention, the process of dynamically modifying the UA information used in the business scenario by the Web browser control in the CefResourceRequestHandler::OnBeforeResourceLoad callback specifically includes: obtaining the HTTP Header of the request object, then modifying the UA field in the Header to the UA customized by the developer, and then sending the data with the modified Header to the server side.

[0014] As a preference of the present invention, in the CefResourceRequestHandler::OnBeforeResourceLoad callback, all HTTP requests are looped until all requests are completed.

[0015] As a preference of the present invention, the pseudocode for the process of dynamically modifying the UA information used in the business scenario by the Web browser control in the CefResourceRequestHandler::OnBeforeResourceLoad callback is: SHMCefHandler::ReturnValue CCefHandler::OnBeforeResourceLoad(CefRefPtr <cefbrowser>browser, CefRefPtr <cefframe>frame, CefRefPtr <cefrequest>request, CefRefPtr <cefcallback>callback) { QString url = request->GetURL().ToString().c_str(); CefRequest::HeaderMap headerMap; request->GetHeaderMap(headerMap); CefRequest::HeaderMap::iterator it_find = headerMap.find("user-agent"); if (it_find != headerMap.end()) { headerMap.erase(it_find); } std::string ua = "costimized_user_agent”; headerMap.insert(std::pair<CefString, CefString>("user-agent",ua)); request->SetHeaderMap(headerMap); return RV_CONTINUE; }。

[0016] A system for dynamically modifying the user agent based on the cef framework, including a Web browser control based on cef, a UA customization module for the web browser created in the Web browser control, a Web browser, and a JS file module.

[0017] The beneficial effects of the present invention: Through the method and system design of this application, developers can modify the UA of the web browser during program operation; they can also use different UAs for different web browsers within one process. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Figure 1 It is a flowchart of the method in Embodiment 1; Figure 2 It is a module diagram of the system in Embodiment 2. DETAILED DESCRIPTION OF THE INVENTION

[0019] The present invention will be further described in detail below with reference to the accompanying drawings.

[0020] This specific embodiment is only an interpretation of the present invention and does not limit the present invention. After reading this specification, those skilled in the art can make modifications to this embodiment without creative contributions as needed, but as long as it is within the scope of the claims of the present invention, it is protected by the patent law.

[0021] Example 1, as Figure 1 shown, a method for dynamically modifying the user agent based on the cef framework includes the following steps: Step 1, create a Web browser control; Step 2, the developer sets the UA of the custom webbrowser in the Web browser control; Step 3, dynamically modify the UA information used in the business scenario through the UA of the custom web browser in Step 2. The solution of this embodiment is designed based on the cef framework. Some of the English and abbreviations involved are explained in advance.

[0022] cef: Chromium Embedded Framework (cef) is an open-source Web browser control based on the Google Chromium project, supporting Windows, Linux, and Mac platforms. In addition to providing C / C++ interfaces, there are also ported versions in other languages.

[0023] UA: User Agent (abbreviated as UA) is a special string header that enables the server to identify the operating system and version used by the client, CPU type, browser and version, browser rendering engine, browser language, browser plug-ins, etc.

[0024] HTTP: Hypertext Transfer Protocol is a simple request-response protocol that usually runs on top of TCP. It specifies what messages the client may send to the server and what responses it will receive.

[0025] URL: Network link, that is, according to the Uniform Resource Locator (URL), using Hypertext Markup Language (HTML), hypertext and hypermedia between internal web pages of a website, between systems, or between different systems are linked.

[0026] JS: JavaScript (abbreviation "JS") is a lightweight, interpreted or just-in-time compiled programming language with function-first. Although it is famous as a scripting language for developing web pages, it is also used in many non-browser environments. JavaScript is a prototype-based, multi-paradigm dynamic scripting language that supports object-oriented, imperative, declarative, and functional programming paradigms.

[0027] The Web browser control in the foregoing solution is a control created under the CEF framework. The original CEF framework provides some secondary development interfaces for developers, allowing developers to handle some business logics in some key running links. Therefore, a small module is configured on the basis of the open-source Web browser control, which is the UA customization module of the web browser, and a new Web browser control of this embodiment is also created. By utilizing the original characteristics of the cef framework in combination with this new module, this control can execute step 3 and can dynamically modify the UA information used in different business scenarios.

[0028] CEF is an open-source Web Browser control of Google Chromium. Based on this control, developers can develop personalized cross-platform browser software or components.

[0029] The solution of this application realizes that the UA of the web browser can be dynamically modified during the operation of the control, and different UAs can be used for different web pages in a program. This solution can be set for a single web browser, and the design is more independent and easy to use.

[0030] The following more specific solution is: to process the UA of a single web browser, and each web browser does not affect each other. In addition, since request interception and JS injection are performed during operation, the UA set by the new control can be preferentially used even when the global UA is set. In this way, developers can set different UAs for multiple web browsers according to needs.

[0031] There are mainly two scenarios for the use of UA. One is that the web server performs different business processes according to the UA; the other is that different business processes are performed according to the UA in the JS file. This solution intercepts and preprocesses these two business scenarios to achieve the purpose of dynamically modifying the UA.

[0032] Specifically, the business scenario includes the web server performing corresponding business processing based on the UA and the JS file performing corresponding business processing based on the UA. And in step 3, the UA information in the business scenario is modified through key event callbacks.

[0033] More specifically, the key event callbacks include the CefLoadHandler::OnLoadStart callback and the CefResourceRequestHandler::OnBeforeResourceLoad callback.

[0034] Among them, the process of the Web browser control dynamically modifying the UA information used in the business scenario in the CefLoadHandler::OnLoadStart callback specifically includes: executing a JS script in the web browser to modify the UA property in the navigator object in the JS running environment of the current page, so as to ensure that when the JS script in the subsequent page obtains the UA through the navigator.userAgent method, the value obtained is the UA value custom-set by the developer.

[0035] The pseudocode for the process of the Web browser control dynamically modifying the UA information used in the business scenario in the CefLoadHandler::OnLoadStart callback is as follows: void CCefHandler::OnLoadStart(CefRefPtr <cefbrowser>browser, CefRefPtr <cefframe>frame, TransitionType transition_type) { std::string ua = "customized_user_agent”; std::string change_ua_script = "Object.defineProperty(navigator, 'userAgent', {get: function () { return '" + ua + "';}});"; frame->ExecuteJavaScript(change_ua_script,frame->GetURL(), 0); }。

[0036] The processing of dynamically modifying the UA information used in the business scenario by the Web browser control in the CefResourceRequestHandler::OnBeforeResourceLoad callback specifically includes: obtaining the HTTPHeader of the request object, then modifying the UA field in the Header to the UA customized by the developer, and then sending the modified Header data to the server side.

[0037] In the CefResourceRequestHandler::OnBeforeResourceLoad callback, all HTTP requests are looped until all requests are completed.

[0038] The pseudocode for the processing of dynamically modifying the UA information used in the business scenario by the Web browser control in the CefResourceRequestHandler::OnBeforeResourceLoad callback is: SHMCefHandler::ReturnValue CCefHandler::OnBeforeResourceLoad(CefRefPtr <cefbrowser>browser, CefRefPtr <cefframe>frame, CefRefPtr <cefrequest>request, CefRefPtr <cefcallback>callback) { QString url = request->GetURL().ToString().c_str(); CefRequest::HeaderMap headerMap; request->GetHeaderMap(headerMap); CefRequest::HeaderMap::iterator it_find = headerMap.find("user-agent"); if (it_find != headerMap.end()) { headerMap.erase(it_find); } std::string ua = "costimized_user_agent”; headerMap.insert(std::pair<CefString, CefString>("user-agent", ua)); request->SetHeaderMap(headerMap); return RV_CONTINUE; }。

[0039] The following is a specific example according to the above solution: In the CEF framework, some secondary development interfaces are provided for developers, allowing them to handle some business logics in some key running processes. This device uses two key event callbacks to process the UA. 1. CefLoadHandler::OnLoadStart callback: This callback occurs before the web browser accesses an address. In this callback, this device executes a piece of JS script in the web browser to modify the userAgent property of the navigator object in the JS running environment of the current page, so as to ensure that when the JS script in the subsequent page obtains the UA through the navigator.userAgent method, the value obtained is the UA value set by the developer himself. Pseudocode: void CCefHandler::OnLoadStart(CefRefPtr <cefbrowser>browser, CefRefPtr <cefframe>frame, TransitionType transition_type) { std::string ua = "customized_user_agent”; std::string change_ua_script = "Object.defineProperty(navigator, 'userAgent', {get: function () { return '" + ua + "';}});"; frame->ExecuteJavaScript(change_ua_script,frame->GetURL(), 0); }。

[0040] 2. CefResourceRequestHandler::OnBeforeResourceLoad This callback occurs before the web browser object executes an HTTP request. In this callback, the device obtains the HTTP header of the request object, then modifies the user-agent field in the header to the developer-customized UA, and then sends the modified header data to the server, thus achieving the purpose of modifying the UA. Pseudocode: SHMCefHandler::ReturnValue CCefHandler::OnBeforeResourceLoad( CefRefPtr <cefbrowser>browser, CefRefPtr <cefframe>frame, CefRefPtr <cefrequest>request, CefRefPtr <cefcallback>callback) { QString url = request->GetURL().ToString().c_str(); CefRequest::HeaderMap headerMap; request->GetHeaderMap(headerMap); CefRequest::HeaderMap::iterator it_find = headerMap.find("user-agent"); if (it_find != headerMap.end()) { headerMap.erase(it_find); } std::string ua = "costimized_user_agent”; headerMap.insert(std::pair<CefString, CefString>("user-agent",ua)); request->SetHeaderMap(headerMap); return RV_CONTINUE; }。

[0041] ‌CefResourceRequestHandler::OnBeforeResourceLoad‌ is an important function in CEF (Chromium Embedded Framework) used to intercept and process before a resource request is loaded. This function allows developers to customize the behavior of handling resource requests, such as redirecting URLs, blocking requests, etc.

[0042] CefLoadHandler::OnLoadStart is a callback function used in Chromium Embedded Framework (CEF) that is called when the browser starts loading page resources. This function is typically used to monitor the page loading process.

[0043] Through these two steps, the UA information used in the front-end running environment and HTTP requests is replaced with the developer-defined value, ultimately achieving the dynamic modification of UA information.

[0044] The process of summarizing the process is as follows: 1. The developer creates a web browser control.

[0045] 2. The developer sets the corresponding UA for the web browser according to the product requirements.

[0046] 3. Execute a script before loading the URL to modify the UA value in the JS running environment.

[0047] 4. By intercepting the HTTP request, modify the UA data in the request header before the web browser requests data from the server.

[0048] 5. Send the modified HTTP request data to the server side.

[0049] 6. Loop through all HTTP requests until all requests are completed.

[0050] Through the above method, the developer can modify the UA of the web browser during program operation; different UAs can also be used for different web browsers within a process. Instead of modifying the UA value from the general UA interface, the UA is processed at the specific usage nodes of the UA, modifying the temporary data, so as to achieve the effect of modifying the UA. The modification is more flexible and can be applied to more diverse browser usage environments.

[0051] Embodiment 2, as Figure 2 shown, a system for dynamically modifying the user agent based on the cef framework, which is applicable to the method in Embodiment 1, specifically including a cef-based Web browser control, a UA customization module for the web browser created in the Web browser control, a Web browser and a JS file module. The cef-based Web browser control provides an overall control framework. The UA customization module for the web browser is used to define the UA information that the developer wants to customize for modification. The Web browser and the JS file module provide a business scenario. Then, the control dynamically modifies the UA information of the browser through callbacks.

[0052] As described above, the above is only the specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art within the technical scope disclosed by the present invention can easily think of various equivalent modifications or substitutions, and these modifications or substitutions should all be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention should be subject to the protection scope of the claims.< / cefcallback> < / cefrequest> < / cefframe> < / cefbrowser> < / cefframe> < / cefbrowser> < / cefcallback> < / cefrequest> < / cefframe> < / cefbrowser> < / cefframe> < / cefbrowser> < / cefcallback> < / cefrequest> < / cefframe> < / cefbrowser> < / cefframe> < / cefbrowser>

Claims

1. A method for dynamically modifying a user agent based on a cef framework, characterized in that: The following steps are involved: Step 1: Create a Web browser control. Step 2: The developer sets the UA of a customized web browser in the Web browser control. Step 3: Dynamically modify the UA information used in the business scenario through the UA of the customized web browser in Step 2.

2. According to a method for dynamically modifying a user agent based on a cef framework according to claim 1, it is characterized in that: The business scenario includes the web server performing corresponding business processing according to the UA and the JS file performing corresponding business processing according to the UA.

3. According to a method for dynamically modifying a user agent based on a cef framework as described in claim 2, it is characterized in that: The UA information in the business scenario is processed and modified through key event callback.

4. According to the method of dynamically modifying user agent based on cef framework according to claim 3, it is characterized in that: The key event callbacks include CefLoadHandler::OnLoadStart callback and CefResourceRequestHandler::OnBeforeResourceLoad callback.

5. According to the method of dynamically modifying user agent based on cef framework according to claim 4, it is characterized in that: The process of dynamically modifying the UA information used in the business scenario in the CefLoadHandler::OnLoadStart callback of the web browser control specifically includes: executing a JS script in the web browser to modify the UA property in the navigator object in the JS runtime environment of the current page, so as to ensure that when the JS script in the subsequent page obtains the UA through the naviagtor.userAgent method, the value obtained is the UA value customized by the developer.

6. A method for dynamically modifying a user agent based on a cef framework according to claim 5, characterized in that: The pseudo code of the process of dynamically modifying the UA information used in the business scenario in the CefLoadHandler::OnLoadStart callback of the Web browser control is as follows: void CCefHandler::OnLoadStart(CefRefPtr <cefbrowser> browser,< / cefbrowser> CefRefPtr <cefframe> frame,< / cefframe> TransitionType transition_type) { std::string ua = "customized_user_agent"; std::string change_ua_script = "Object.defineProperty(navigator, 'userAgent', {get: function () { return '" + ua + "';}});"; frame->ExecuteJavaScript(change_ua_script,frame->GetURL(), 0); }。 7. According to claim 5, a method for dynamically modifying a user agent based on a cef framework is characterized in that: The process of dynamically modifying the UA information used in the business scenario in the CefResourceRequestHandler::OnBeforeResourceLoad callback of the Web browser control specifically includes: obtaining the HTTP Header of the request object, then modifying the UA field in the Header to the UA customized by the developer, and then sending the modified Header data to the server.

8. A method for dynamically modifying a user agent based on a cef framework according to claim 6, characterized in that: In the CefResourceRequestHandler::OnBeforeResourceLoad callback, loop all HTTP requests until all requests are completed.

9. A method for dynamically modifying a user agent based on a cef framework according to claim 6, characterized in that: The pseudo code of the process of dynamically modifying the UA information used in the business scenario in the CefResourceRequestHandler::OnBeforeResourceLoad callback of the Web browser control is as follows: SHMCefHandler::ReturnValueCCefHandler::OnBeforeResourceLoad(CefRefPtr <cefbrowser> browser,< / cefbrowser> CefRefPtr <cefframe> frame,< / cefframe> CefRefPtr <cefrequest> request,< / cefrequest> CefRefPtr <cefcallback> callback) {< / cefcallback> QString url = request->GetURL().ToString().c_str(); CefRequest::HeaderMap headerMap; request->GetHeaderMap(headerMap); CefRequest::HeaderMap::iterator it_find = headerMap.find("user-agent"); if (it_find != headerMap.end()) { headerMap.erase(it_find); } std::string ua = "costimized_user_agent"; headerMap.insert(std::pair<CefString, CefString> ("user-agent",ua)); request->SetHeaderMap(headerMap); return RV_CONTINUE; }。 10. A system for dynamically modifying user agents based on the cef framework, characterized in that: It includes a cef-based Webbrowser control, a UA custom module of a web browser created in the Web browser control, a Web browser and a JS file module.