A method for extracting CSS style of the first screen of a web page

By configuring a Node.js server and a headless browser to extract the CSS styles of the first screen of a webpage, and combining PostCSS filtering and asynchronous loading, the problem of insufficient CSS optimization in the later stages of webpage development is solved, thereby improving webpage performance and loading speed.

CN112231610BActive Publication Date: 2024-11-08NANJING FOCUS LINGDONG CLOUD COMPUTING TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011220052.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-11-05
Publication Date
2024-11-08
Estimated Expiration
2040-11-05

AI Technical Summary

Technical Problem

Existing technologies lack CSS optimization methods after web page development, resulting in limited performance improvements for web pages.

Method used

By configuring the Node server, a headless browser is launched to obtain the CSS styles of the first screen of the webpage. After filtering with PostCSS, non-first screen CSS styles are loaded asynchronously. The first screen CSS styles are analyzed in conjunction with the virtual device width, and then returned to the server for re-rendering.

Benefits of technology

After development is complete, further improve webpage performance, reduce HTTP requests and DOM content, and increase first-screen loading speed.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112231610B_ABST
    Figure CN112231610B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for extracting CSS styles of the first screen of a web page, which is characterized by comprising step one, configuring a node server, step two: initiating a request for extracting CSS styles through a client, starting a non-interface browser and starting an extraction task, and waiting for the non-interface browser to return a result; step three: configuring nodejs access parameters, calling a collection method to obtain all CSS styles in a target page; step four: using postcss to filter all CSS styles in the page obtained in step three; step five: analyzing the CSS styles of the first screen; step six: re-rendering the CSS styles of the first screen, so as to achieve performance optimization after the development of the main body is completed, and further improve the performance; extracting the main CSS content, removing link and style tags to reduce http requests and doc content, and having a significant effect on improving the performance of web pages with more page content.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the service discovery field of computer processing technology, and in particular to a method for extracting CSS styles of the first screen of a web page. Background Art

[0002] In the computer field, the amount of web page content is very important to developers, testers, users and service personnel. In modern browser systems, in order to avoid the problem of slow web page loading, poor user experience and user loss caused by too much DOC content, developers often pay attention to this issue at the beginning of development, such as using the new HTML5 protocol, reducing the nesting level of tags as much as possible, and delaying the loading and execution of js as much as possible. These are some traditional methods to optimize web pages.

[0003] Most of the traditional methods are to optimize web pages during the development phase. For example, execute js asynchronously to prevent blocking rendering; reduce operations on DOM to reduce performance loss; reduce the number of HTTP requests to make the browser respond faster; cache ajax requests, and resources that have not expired should be read directly from the cache; reduce the number of DOMs, use more semantic tags, and use as little or no table layout as possible.

[0004] As mentioned above, most of the above solutions require attention to performance during the development phase, and will not be able to further optimize performance and DOC size after development is completed. And most of the optimizations are for DOM and JS, and there are few optimization solutions for CSS. CCS Cascading Style Sheets (full name: CascadingStyle Sheets) is a computer language used to express file styles such as HTML (an application of standard general purpose markup language) or XML (a subset of standard general purpose markup language). CSS can not only modify web pages statically, but also dynamically format various elements of web pages in conjunction with various scripting languages.

[0005] Therefore, a solution is needed to provide developers with a further performance improvement solution at the development completion stage, so that web page performance can be further improved and the number of CSS external links and CSS can be reduced. Summary of the invention

[0006] The technical problem to be solved by the present invention is to overcome the deficiencies of the prior art and provide a method for extracting the CSS style of the first screen of a web page.

[0007] In order to solve the above technical problems, the present invention provides a method for extracting the style of the CSS of the first screen of a web page, which is characterized by comprising the following steps:

[0008] Step 1: Configure a node server, which is used to receive a request to extract CSS styles;

[0009] Step 2: The client initiates a request to extract the CSS style. After receiving the request, the node server starts the interfaceless browser and starts the extraction task. The extraction task means that the node server starts a listening event after receiving the request from the client. The listening event is used to obtain the CSS used in the first screen of the page and wait for the interfaceless browser to return the result.

[0010] Step 3: Configure nodejs access parameters, and configure the access address of the interfaceless browser according to the address carried by the client; open the interfaceless browser to access the target page, and after the target page is fully loaded, call the collection method to obtain all CSS styles in the target page, and the collection method includes: using javascript to obtain the link tag content and the style tag content;

[0011] Step 4: Use postcss to filter all CSS styles in the page obtained in step 3;

[0012] Step 5: Get all CSS styles in the page according to step 4, analyze the CSS style of the first screen according to the first screen style ratio provided by the headless browser and the set virtual device width, return the CSS style of the first screen to the task started by the node server in the form of a promise call, close the headless browser process, and wait for subsequent requests;

[0013] Step 6: Re-render the CSS style of the first screen. Specifically, put the CSS style of the first screen into the document and use JavaScript to delay the loading of other styles.

[0014] In the step 1, the configuration content of the node server includes: the number of interfaceless browsers started simultaneously, the width and height of the device and the startup mode; the request includes the page access address from which the CSS style needs to be extracted.

[0015] In the step three, when the interfaceless browser is Google Chrome, communication is performed according to CDPSession, and the extracted CSS style is passed into the extraction method to generate the CSS style of the first screen of the page. The extraction method includes setNetwork, setDom and setPage.

[0016] In the step 5, the first screen style ratio refers to the proportion of CSS styles in the first screen, which is obtained according to the communication protocol between the headless browser and the Chrome DevTool Protocol. The analysis of the CSS style of the first screen is specifically as follows: according to the first screen style ratio and the width set to the virtual device, all styles of the target address are traversed, and it is determined whether it is used in the first screen according to the maximum width and the minimum width. If it is not used in the first screen, it is removed.

[0017] The step six also includes: using JavaScript to asynchronously load other styles.

[0018] The beneficial effects achieved by the present invention are as follows: after the main body of the development is completed, performance optimization can be performed to further improve performance; the main CSS content is extracted, link and style tags are removed to reduce http requests and doc content, and the performance of web pages with a lot of page content is significantly improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 The following is a simplified flowchart of a method according to an exemplary embodiment of the present invention. DETAILED DESCRIPTION

[0020] The present invention will be further described below in conjunction with the accompanying drawings and exemplary embodiments:

[0021] like Figure 1 A method for extracting the CSS style of the first screen of a web page is shown, and the specific steps are:

[0022] Step S1: configure node server parameters, the node server is used to receive requests for extracting CSS styles; the configuration of the node server includes configuring: the number of interfaceless browsers started simultaneously, the width and height of the device, and the startup mode; the request includes the page access address from which the CSS style needs to be extracted.

[0023] Step S2: The client initiates a request to extract the CSS style. After receiving the request from the client, the node server starts the interfaceless browser and starts the extraction task. The extraction task means that after receiving the request from the client, the node server starts a monitoring event. The monitoring event is used to obtain the CSS with the highest proportion in the first screen of the page and wait for the interfaceless browser to return the result.

[0024] When the interfaceless browser is Google Chrome, communication is performed according to CDPSession, the CSS style used in the first screen is extracted, and the extracted CSS style is passed into the extraction method to generate the style required for the first screen of the page. The extraction method includes setNetwork, setDom and setPage; the tags include style tags and link tags.

[0025] Step S3: configure nodejs access parameters. Configuring node access parameters here refers to configuring the access address of the interfaceless browser according to the address carried by the client, starting the task. Starting the task here refers to starting the interfaceless browser to start accessing the target address, accessing the target page, and calling the collection method after the page is fully loaded. The collection method refers to the method of javascript obtaining the link tag content and the style tag content, and obtaining all CSS styles in the page; in the step S3, when the interfaceless browser is Google Chrome, communication is performed according to CDPSession to extract the CSS style of the target page, and the extracted CSS style is passed into the extraction method to generate the CSS style of the first screen of the page. The extraction method includes setNetwork, setDom and setPage;

[0026] Step S4: After obtaining all the styles in the page in step S3, use the method in postcss to filter them. In order to prevent illegal CSS writing methods from causing errors during the filtering process; re-rendering specifically includes: putting the obtained first-screen CSS style into doc, and using js to delay loading other styles, ensuring that less content is displayed to users for the first time and faster rendering speed. Subsequent asynchronous loading of styles using js also ensures the integrity of content outside the first screen.

[0027] Step S5: Get all CSS styles according to step S4. If all CSS styles in the page are not successfully obtained, directly return empty content to the node server. Otherwise, analyze the CSS style of the first screen according to the first screen style ratio provided by the interfaceless browser and the set virtual device width. The analysis of the CSS style of the first screen is specifically as follows: according to the first screen style ratio and the virtual device width set, traverse all styles of the target address, and determine whether they are used in the first screen according to the maximum width and minimum width. If they are not used in the first screen, remove them. Then return the CSS style of the first screen to the task opened by the node server in the form of a promise call, close the interfaceless browser process, and wait for subsequent requests; the virtual device width is mainly to extract the required styles more accurately. The main operation is to filter the CSS style in the first screen according to the set virtual device width to filter the styles that are no longer in this range of media queries. In this way, the CSS style of the first screen is obtained.

[0028] Step S6: Re-render the CSS style of the first screen. Specifically, the client puts the received CSS style of the first screen into the document and uses JavaScript to delay the loading of other styles. This ensures that less content is displayed to the user for the first time and the rendering speed is faster. The subsequent use of js to asynchronously load the style also ensures the integrity of the content outside the first screen.

[0029] Another exemplary embodiment of the present invention may require the user to predefine the device width and height to be extracted, the extracted device (PC / Mobile), and the CSS rules for delaying the loading of non-first screen access, including the following steps:

[0030] Step 1: The node server configures the number of headless browsers to be started simultaneously, the device width and height, and the startup mode (for example, 20 headless browsers can be started at the same time to extract CSS styles. The device width and height are set to the width and height of iPhone 6, which are width: 375px; height: 475px; the startup mode is no cache mode.)

[0031] Step 2: The client starts to initiate a request to extract the CSS website, with the main parameter being the address of the page to be requested. The Node application receives the request and starts a headless browser, and starts a task to wait for the style results extracted by the headless browser.

[0032] Step 3: After the node starts a task, it then starts calling the style collection method. The main implementation of this method is to use the CDPSession instance for native communication with the Chrome Devtools protocol. The protocol method mainly tells the browser to start the NetWork, Page, CSS and other communication methods (for example, after the Network method starts the NetWork method, it can mainly monitor the request parameters, cache, cookies and other information. After the Page method starts, it can monitor whether the page has finished loading, so as to mark whether the style collection operation can be performed to prevent the collection from starting before the page is loaded, resulting in incorrect collected styles. The CSS communication method is mainly to obtain some styles mainly used in the first screen.)

[0033] Step 4: Get all the links and styles in the page. Use the postcss method to filter to prevent CSS errors during formatting. Since the style returned by the interfaceless browser is a collection of ids, you need to extract the corresponding css for this collection separately.

[0034] Step 5: After getting all the styles in the page, get the final main style of the first screen according to the proportion of the first screen style of the Google headless browser and the set virtual device width. Finally, return the style to the task in the form of a promise call, and the task closes a headless browser process, and the subsequent waiting requests will continue to execute.

[0035] Step 6: Put the acquired first screen style into doc, and use js to delay loading of other styles, so as to ensure that less content is displayed to users for the first time and faster rendering speed. The subsequent use of js to asynchronously load the style also ensures the integrity of the content outside the first screen.

[0036] The present invention is mainly used to provide a method for extracting the CSS style of the first screen of a web page. After the development of the main body is completed, performance optimization is performed to further improve the performance; the main CSS content is extracted, link and style tags are removed to reduce http requests and doc content, and the performance of web pages with much page content is significantly improved.

[0037] The above embodiments do not limit the present invention in any way. Any other improvements and applications made to the above embodiments in an equivalent transformation manner belong to the protection scope of the present invention.

Claims

1. A method for extracting the CSS style of the first screen of a web page, characterized in that: The steps include: Step 1: Configure a node server, which is used to receive a request to extract CSS styles; Step 2: The client initiates a request to extract the CSS style. After receiving the request, the node server starts the interfaceless browser and starts the extraction task. The extraction task means that the node server starts a listening event after receiving the request from the client. The listening event is used to obtain the CSS used in the first screen of the page and wait for the interfaceless browser to return the result. Step 3: Configure the nodejs access parameters and configure the access address of the interfaceless browser according to the address carried by the client; Open the interfaceless browser to access the target page, and after the target page is completely loaded, call the collection method to obtain all CSS styles in the target page, the collection method includes: using javascript to obtain the link tag content and the style tag content; When the interfaceless browser is Google Chrome, the communication is performed according to CDPSession, and the extracted CSS style is passed into the extraction method to generate the CSS style of the first screen of the page, and the extraction method includes setNetwork, setDom and setPage; Step 4: Use postcss to filter all CSS styles in the page obtained in step 3; Step 5: Get all CSS styles in the page according to step 4, analyze the CSS style of the first screen according to the first screen style ratio provided by the headless browser and the set virtual device width, return the CSS style of the first screen to the task started by the node server in the form of a promise call, close the headless browser process, and wait for subsequent requests; The first screen style ratio refers to the proportion of CSS styles in the first screen, which is obtained according to the communication protocol between the headless browser and the ChromeDevTool Protocol. The analysis of the CSS style of the first screen is specifically as follows: according to the first screen style ratio and the width set to the virtual device, all styles of the target address are traversed, and whether they are used in the first screen is determined according to the maximum width and the minimum width. If they are not used in the first screen, they are removed; Step 6: Re-render the CSS style of the first screen. Specifically, put the CSS style of the first screen into the document and use JavaScript to delay the loading of other styles.

2. A method for extracting CSS style of the first screen of a web page as claimed in claim 1, characterized in that: In the step 1, the configuration content of the node server includes: the number of interfaceless browsers started simultaneously, the width and height of the device and the startup mode; the request includes the page access address from which the CSS style needs to be extracted.

3. A method for extracting the CSS style of the first screen of a web page as claimed in claim 2, characterized in that: The step six also includes: using JavaScript to asynchronously load other styles.