Local resource loading method and related device
By registering a custom protocol and adding tags in the Electron application, the problem of the browser's same origin policy restricting the loading of local resources is solved, and safe and flexible loading of local resources is achieved to adapt to the needs of different environments.
Patent Information
- Application Number
- CN202410907098.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-08
- Publication Date
- 2024-08-23
AI Technical Summary
In Electron applications, local resources cannot be loaded due to the browser's same-origin policy restrictions. Disabling the same-origin policy may cause security issues.
By registering a custom protocol in the Electron application, adding a custom protocol tag to the access path of local resources, and calling the callback handler function to convert it into a resource path that conforms to the custom protocol, local resources can be loaded by bypassing the same-origin policy restrictions.
It enables loading of local resources without disabling the browser's same-origin policy, avoiding security risks, improving the flexibility and scalability of Electron applications, and supporting offline operation and local resource management.
Smart Images

Figure CN118535248A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and more specifically, to a local resource loading method and related devices. Background Art
[0002] Electron is a framework that can be used to build cross-platform desktop applications. It allows developers to create desktop applications using Web technologies such as JavaScript, HTML, and CSS.
[0003] In Electron applications, local resources need to be loaded sometimes, such as loading local music for playback, loading local HTML for display on the page, etc. However, due to security considerations, the browser's homology policy disables the file: / / protocol for external file display when loading files on the web page, resulting in the inability to load local resources.
[0004] Although Electron allows developers to disable the browser's same-origin policy to load local resources, disabling the browser's same-origin policy may lead to a series of security issues. Summary of the invention
[0005] In view of the above problems, this application provides a local resource loading method and related devices to achieve the purpose of local resource loading in Electron applications. The specific scheme is as follows:
[0006] The first aspect of the present application provides a local resource loading method, comprising:
[0007] In response to a resource loading request from an Electron application, determining whether an access path of a target resource corresponding to the resource loading request is a local path;
[0008] If the access path of the target resource is a local path, adding a custom protocol tag to the access path of the target resource;
[0009] Calling the callback processing function of the custom protocol to convert the access path marked with the custom protocol into a resource path that complies with the custom protocol;
[0010] The target resource is loaded according to the resource path.
[0011] In a possible implementation, before responding to the resource loading request of the Electron application, it also includes:
[0012] When the Electron application is in the application ready state, the custom protocol is registered, and the name and callback processing function of the custom protocol are set.
[0013] In a possible implementation, adding a custom protocol tag to the access path of the target resource includes:
[0014] The name of the custom protocol and : / / are concatenated before the access path of the target resource.
[0015] In a possible implementation, the calling callback processing function of the custom protocol converts the access path marked with the custom protocol into a resource path that complies with the custom protocol, including:
[0016] Calling the URL parsing function in the callback processing function to parse the access path of the target resource to obtain the URL without the protocol name;
[0017] Calling the normalization function in the callback processing function to normalize the URL to obtain a normalized URL;
[0018] Calling the decoding function in the callback processing function to decode the normalized URL into the resource path;
[0019] Calling the callback function in the callback processing function returns the resource path.
[0020] In a possible implementation, determining whether the access path of the target resource corresponding to the resource loading request is a local path includes:
[0021] Parsing the resource loading request to obtain an access path to the target resource;
[0022] According to the protocol name in the access path of the target resource, it is determined whether the access path of the target resource is a local path.
[0023] A second aspect of the present application provides a local resource loading device, including:
[0024] A determination unit, configured to determine, in response to a resource loading request of an Electron application, whether an access path of a target resource corresponding to the resource loading request is a local path;
[0025] A path processing unit, configured to add a custom protocol tag to the access path of the target resource if the access path of the target resource is a local path;
[0026] A path conversion unit, used to call the callback processing function of the custom protocol, and convert the access path marked with the custom protocol into a resource path that conforms to the custom protocol;
[0027] A resource loading unit is used to load the target resource according to the resource path.
[0028] In a possible implementation, the local resource loading device further includes:
[0029] The protocol registration unit is used to register the custom protocol and set the name and callback processing function of the custom protocol when the Electron application is in the application ready state.
[0030] A third aspect of the present application provides a computer program product, comprising computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements the local resource loading method of the first aspect or any implementation of the first aspect.
[0031] A fourth aspect of the present application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:
[0032] The memory is used to store computer programs;
[0033] The processor is used to execute the computer program so that the electronic device can implement the local resource loading method of the above-mentioned first aspect or any implementation manner of the first aspect.
[0034] A fifth aspect of the present application provides a computer storage medium, which carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can load local resources according to the first aspect or any implementation method of the first aspect.
[0035] By means of the above technical scheme, the present application provides a local resource loading method and related devices. If the access path of the target resource corresponding to the resource loading request is a local path, a custom protocol tag is added to the access path of the target resource, and the callback processing function of the custom protocol is called to convert the access path with the custom protocol tag into a resource path that conforms to the custom protocol, thereby avoiding the target resource being identified as a local resource and restricted from loading by the browser's same-origin policy, thereby bypassing the browser's same-origin policy restrictions and realizing local resource loading in Electron applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0037] Figure 1 A schematic diagram of a system architecture provided for this application;
[0038] Figure 2A schematic diagram of the structure of a terminal 100 provided in this application;
[0039] Figure 3 A schematic diagram of the structure of a server 200 provided in this application;
[0040] Figure 4 A flowchart of a local resource loading method provided in an embodiment of the present application;
[0041] Figure 5 A schematic diagram of the structure of a local resource loading device provided in an embodiment of the present application;
[0042] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0043] The following describes the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. The terms used in the implementation method section of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.
[0044] The embodiments of the present application are described below in conjunction with the accompanying drawings. Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0045] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, which is only to describe the distinction mode adopted by the objects of the same attributes when describing in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0046] In order to more clearly illustrate the technical solution of the present application, the relevant concepts involved in the present application are explained below.
[0047] Electron: is a framework for building desktop applications using JavaScript, HTML, and CSS. Electron can run on Windows, macOS, Linux, and other operating systems. Since Electron is built on Chromium and Node.js, it can take advantage of the cross-platform features of these two frameworks, allowing applications written by developers to run on the above operating systems without major modifications.
[0048] Same-origin policy: It is a browser security mechanism that limits how a web document or script can interact with resources from other sources.
[0049] Electron's main process: In Electron, the process that runs the main script in package.json is called the main process. The script running in the main process can display the GUI (Graphical User Interface) in the form of creating web pages. The main process creates web pages by creating browser window instances. Each browser window instance runs a web page in its rendering process. When a BrowserWindow instance is destroyed, the corresponding rendering process is also terminated. The main process manages all web pages and their corresponding rendering processes.
[0050] Renderer Process: Since Electron uses Chromium to display pages, Chromium's multi-process structure is fully utilized. Each Electron page runs its own process, which is called a renderer process. A renderer process can only manage a single web page, and a crash in one renderer process will not affect other renderers.
[0051] See also Figure 1 , Figure 1 A schematic diagram of a system architecture is shown. The system may include a terminal 100 and a server 200. The server 200 may include one or more servers ( Figure 1 In the example of including a server), the terminal 100 may provide the method provided in the embodiment of the present application.
[0052] Among them, an Electron application can be installed on the terminal 100, and the above application can provide an interface. The terminal 100 can receive relevant parameters entered by the user on the interface, trigger local resource loading or trigger network resource loading, and send the above parameters to the server 200. The server 200 can obtain processing results based on the received parameters and return the processing results to the terminal 100. For example: the server 200 responds to the network resource loading request sent by the terminal 100 and returns the network resources corresponding to the network resource loading request.
[0053] It should be understood that in some optional implementations, the terminal 100 can also complete the action of obtaining the processing result based on the received parameters by itself without the cooperation of the server. The embodiments of the present application are not limited to this. For example: the terminal 100 triggers the loading of local resources based on the received parameters to realize the loading of local resources. In this case, no server cooperation is required.
[0054] Next describe Figure 1 The product form of the mid-terminal 100;
[0055] The terminal 100 in the embodiment of the present application may be a tablet computer, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, etc., and the embodiment of the present application does not impose any limitation on this.
[0056] Figure 2 An optional hardware structure diagram of the terminal 100 is shown.
[0057] refer to Figure 2 As shown, the terminal 100 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160 (optional), a speaker 161 (optional), a microphone 162 (optional), an earphone jack 163 (optional), a processor 170, an external interface 180, a power supply 190 and other components. Those skilled in the art will appreciate that Figure 2 This is merely an example of a terminal and does not constitute a limitation on the terminal. The terminal may include more or fewer components than shown in the figure, or a combination of certain components, or different components.
[0058] The input unit 130 can be used to receive input digital or character information, and generate key signal input related to the user settings and function control of the terminal. Specifically, the input unit 130 may include a touch screen 131 (optional) and / or other input devices 132. The touch screen 131 can collect the user's touch operations on or near it (such as the user's operation on or near the touch screen using any suitable object such as fingers, joints, stylus, etc.), and drive the corresponding connection device according to a pre-set program. The touch screen can detect the user's touch action on the touch screen, convert the touch action into a touch signal and send it to the processor 170, and can receive and execute the command sent by the processor 170; the touch signal at least includes the touch point coordinate information. The touch screen 131 can provide an input interface and an output interface between the terminal 100 and the user. In addition, the touch screen can be implemented using multiple types such as resistive, capacitive, infrared and surface acoustic wave. In addition to the touch screen 131, the input unit 130 can also include other input devices. Specifically, other input devices 132 may include, but are not limited to, one or more of a physical keyboard, function keys (such as a volume control key, a switch key, etc.), a trackball, a mouse, a joystick, and the like.
[0059] Among them, the input device 132 can receive input data and the like.
[0060] The display unit 140 may be used to display information input by the user or provided to the user, various menus of the terminal 100, interactive interfaces, file display, and / or playback of any multimedia file. In an embodiment of the present application, the display unit 140 may be used to display the interface of the Electron application, resource loading results, etc.
[0061] The memory 120 can be used to store instructions and data. The memory 120 can mainly include an instruction storage area and a data storage area. The data storage area can store various data, such as multimedia files, texts, etc.; the instruction storage area can store software units such as operating systems, applications, instructions required for at least one function, or their subsets and extensions. It can also include a non-volatile random access memory; provide the processor 170 with hardware, software and data resources including management of computing and processing equipment, and support control software and applications. It is also used for the storage of multimedia files, and the storage of running programs and applications.
[0062] The processor 170 is the control center of the terminal 100. It uses various interfaces and lines to connect various parts of the entire terminal 100. By running or executing instructions stored in the memory 120 and calling data stored in the memory 120, it executes various functions of the terminal 100 and processes data, thereby controlling the terminal device as a whole. Optionally, the processor 170 may include one or more processing units; preferably, the processor 170 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface and application program, and the modem processor mainly processes wireless communication. It is understandable that the above-mentioned modem processor may not be integrated into the processor 170. In some embodiments, the processor and the memory may be implemented on a single chip, and in some embodiments, they may also be implemented separately on separate chips. The processor 170 may also be used to generate corresponding operation control signals, send them to corresponding components of the computing and processing device, read and process data in the software, especially read and process data and programs in the memory 120, so that each functional module therein performs corresponding functions, thereby controlling the corresponding components to act according to the requirements of the instructions.
[0063] Among them, the memory 120 can be used to store software codes related to the local resource loading method, the processor 170 can execute the steps of the local resource loading method, and can also schedule other units (such as the above-mentioned input unit 130 and display unit 140) to implement corresponding functions.
[0064] The radio frequency unit 110 (optional) can be used for receiving and sending information or receiving and sending signals during a call, for example, after receiving the downlink information of the base station, it is sent to the processor 170 for processing; in addition, the designed uplink data is sent to the base station. Generally, the RF circuit includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (Low Noise Amplifier, LNA), a duplexer, etc. In addition, the radio frequency unit 110 can also communicate with network devices and other devices through wireless communication. The wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (Global System of Mobile communication, GSM), General Packet Radio Service (General Packet Radio Service, GPRS), Code Division Multiple Access (Code Division Multiple Access, CDMA), Wideband Code Division Multiple Access (Wideband Code Division Multiple Access, WCDMA), Long Term Evolution (Long Term Evolution, LTE), email, Short Messaging Service (SMS), etc.
[0065] In this embodiment of the present application, the RF unit 110 can send data to the server 200 and receive processing results sent by the server 200.
[0066] It should be understood that the radio frequency unit 110 is optional and can be replaced by other communication interfaces, such as a network port.
[0067] The terminal 100 also includes a power supply 190 (such as a battery) for supplying power to various components. Preferably, the power supply can be logically connected to the processor 170 through a power management system, so that the power management system can manage functions such as charging, discharging, and power consumption.
[0068] The terminal 100 further includes an external interface 180 , which may be a standard Micro USB interface or a multi-pin connector, and may be used to connect the terminal 100 to communicate with other devices, or to connect a charger to charge the terminal 100 .
[0069] Although not shown, the terminal 100 may also include a flashlight, a wireless fidelity (WiFi) module, a Bluetooth module, sensors with different functions, etc., which are not described in detail here. Some or all of the methods described below may be applied in the following embodiments. Figure 2 In the terminal 100 shown.
[0070] Next describe Figure 1 The product form of the server 200;
[0071] Figure 3 A structural diagram of a server 200 is provided, such as Figure 3 As shown, the server 200 includes a bus 201, a processor 202, a communication interface 203, and a memory 204. The processor 202, the memory 204, and the communication interface 203 communicate with each other via the bus 201.
[0072] The bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0073] The processor 202 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0074] The memory 204 may include a volatile memory, such as a random access memory (RAM). The memory 204 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard drive (HDD), or a solid state drive (SSD).
[0075] The memory 204 may be used to store software codes related to the network resource loading method, and the processor 202 may execute the steps of the network resource loading method of the chip, and may also schedule other units to implement corresponding functions.
[0076] It should be understood that the above-mentioned terminal 100 and server 200 can be centralized or distributed devices, and the processors in the above-mentioned terminal 100 and server 200 (such as processor 170 and processor 202) can be hardware circuits (such as application specific integrated circuit (ASIC), field-programmable gate array (FPGA), general-purpose processor, digital signal processor (DSP), microprocessor or microcontroller, etc.), or a combination of these hardware circuits. For example, the processor can be a hardware system with an instruction execution function, such as a CPU, DSP, etc., or a hardware system without an instruction execution function, such as an ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without an instruction execution function and hardware systems with an instruction execution function.
[0077] Currently, in Electron applications, the browser's same-origin policy, for security reasons, disables the file: / / protocol for external file display when loading files on a web page, resulting in the inability to load local resources. Electron allows developers to disable the browser's same-origin policy through bPreferences.webSecurity: false to enable loading of local resources. In Electron, WebPreferences is an option object that is used to configure various preferences for the rendering process (that is, the window where the web page content is located). When developers use the BrowserWindow class to create a new window, they can set this object through the webPreferences property. Among them, webSecurity controls whether the security features of the web page are enabled. Setting webSecurity to false will disable the browser's same-origin policy.
[0078] However, disabling the browser's same-origin policy may lead to the following security issues:
[0079] Cross-site scripting (XSS): Disabling the browser's same-origin policy can make the application vulnerable to cross-site scripting attacks. Malicious scripts can be injected into the application to steal sensitive user information or perform malicious actions.
[0080] Cross-site request forgery (CSRF): Disabling the browser's same-origin policy may also make your application vulnerable to cross-site request forgery attacks, where an attacker can masquerade as a legitimate user and perform unauthorized actions.
[0081] Mixed content attacks: Disabling the browser's same-origin policy can cause applications to load insecure mixed content, which can be exploited by attackers to perform man-in-the-middle attacks or steal user information.
[0082] Security vulnerability exploits: Disabling the browser's same-origin policy may make the application vulnerable to various security vulnerabilities, including but not limited to clickjacking, DNS rebinding attacks, etc.
[0083] In summary, for the sake of application security, the browser's same-origin policy is generally not disabled. Therefore, how to load local resources without disabling the browser's same-origin policy has become a technical problem that needs to be solved urgently in this field.
[0084] In order to solve the above technical problems, the present application provides a local resource loading method. A local resource loading method of an embodiment of the present application is described in detail below in conjunction with the accompanying drawings.
[0085] Reference Figure 4 , Figure 4 A flow chart of a local resource loading method provided in an embodiment of the present application is as follows: Figure 4 As shown, a local resource loading method provided in an embodiment of the present application may include steps 401 to 404, and these steps are described in detail below.
[0086] 401: In response to a resource loading request from an Electron application, determine whether the access path of the target resource corresponding to the resource loading request is a local path;
[0087] The target resource corresponding to the resource loading request can be pictures, audio, video, CSS (Cascading Style Sheets), JavaScript scripts, HTML web pages, etc.
[0088] Resource loading requests can be triggered by users, for example, users load local music for playback.
[0089] Resource loading requests can also be automatically triggered by Electron applications. For example, a user downloads a skin theme to the local computer, and the Electron application changes the skin based on the images and other resources in it. Another example is that the Electron application needs to load the local HTML to display on the page.
[0090] The Electron rendering process responds to the resource loading request of the Electron application and determines whether the access path of the target resource corresponding to the resource loading request is a local path. Among them, a possible implementation method for determining whether the access path of the target resource corresponding to the resource loading request is a local path is: parsing the resource loading request, obtaining the access path of the target resource, and determining whether the access path of the target resource is a local path according to the protocol name in the access path of the target resource. For example: file: / / D:\Image\demo.png is the access path of the png picture named demo in the Image folder of the terminal D drive. According to the protocol name file in the access path, it can be determined that the access path is a local path and the png picture named demo is a local resource.
[0091] If the access path of the target resource is not a local path, execute 402: load the target resource according to the access path of the target resource;
[0092] Exemplarily, if the access path of the target resource is not a local path, the Electron rendering process is called to load the target resource according to the access path of the target resource.
[0093] If the access path of the target resource is a local path, execute 403: add a custom protocol tag to the access path of the target resource;
[0094] Custom protocols need to be pre-registered. Custom protocols are used to intercept resource loading requests and call callback processing functions to return resource paths that can be correctly parsed by the Electron rendering process.
[0095] Exemplarily, the custom protocol tag is the name of the custom protocol and : / / .
[0096] Taking the access path of the target resource as file: / / D:\Image\demo.png as an example, the custom protocol tag is concatenated before the access path to obtain atom: / / file: / / D:\Image\demo.png, where atom is the custom protocol name.
[0097] 404: Call the callback processing function of the custom protocol to convert the access path marked with the custom protocol into a resource path that conforms to the custom protocol;
[0098] The callback processing function of the custom protocol is set when registering the custom protocol. It is used to remove the protocol name in the access path, normalize and decode the URL without the protocol name, and return the resource path that conforms to the custom protocol.
[0099] 405: Load the target resource according to the resource path.
[0100] Call the Electron rendering process and load the target resource according to the resource path.
[0101] The present embodiment discloses a local resource loading method. If the access path of the target resource corresponding to the resource loading request is a local path, a custom protocol tag is added to the access path of the target resource, and the callback processing function of the custom protocol is called to convert the access path with the custom protocol tag into a resource path that conforms to the custom protocol, so as to avoid the target resource being identified as a local resource and restricted from loading by the browser's homology policy, thereby bypassing the browser's homology policy restriction and realizing local resource loading in the Electron application.
[0102] Using custom protocols can achieve direct access to local resources without going through HTTP or other network protocols. It can avoid the security risks brought by turning off the same-origin policy, which may lead to security issues such as cross-site scripting attacks (XSS). Using custom protocols can access local resources without reducing security.
[0103] Bypassing the same-origin policy restriction means that local resources can be loaded in Electron applications without worrying about cross-domain security issues. This makes it easier to develop local applications without being affected by cross-domain restrictions.
[0104] Accessing local resources through custom protocols can ensure that Electron applications can run normally without a network connection, without relying on external network resources. This provides better support for developing offline applications, allowing Electron applications to run normally in various environments, and can also implement some specific functions, such as loading local configuration files, reading local databases, etc. This can improve the flexibility and scalability of Electron applications, allowing Electron applications to better adapt to different needs and provide developers with more customization options.
[0105] It should be noted that the custom protocol in the above embodiment needs to be pre-registered. This embodiment introduces the app module and the protocol module in the main process of the Electron application. The app module provides the life cycle management of the Electron application and some global properties and methods. The protocol module is used to register and use the custom protocol in the Electron application. In this embodiment, the main function of the app module is to listen to ready. Ready is a life cycle event. When the Electron application completes initialization and is ready to create a browser window, the ready event will be triggered. After the ready event is triggered, the Electron application is in the application ready state. In other words, the custom protocol is registered when the Electron application is in the application ready state. By pre-registering the custom protocol, it is ensured that the Electron application has registered the custom protocol when accessing local resources, avoiding the problem of failing to load local resources due to accessing resources first and then registering the custom protocol.
[0106] For example, the registerFileProtocol method of the protocol module is used to register a custom protocol. The following is the analysis and example usage of the protocol.registerFileProtocol method:
[0107] protocol.registerFileProtocol(scheme, handlerFunction).
[0108] Among them, scheme (String) indicates the name of the custom protocol to be registered, such as 'atom'. handlerFunction (Function) indicates the function that will be called when a link using the custom protocol is accessed. The function includes two parameters: request (Object) and callback (Function).
[0109] request (Object) represents the object of the requested resource, callback (Function) is a callback function that returns the resource path converted by the callback processing function. The parameter received by callback (Function) is path (String), which points to the access path of the requested resource.
[0110] In a possible implementation, step 404 in the above embodiment: calling the callback processing function of the custom protocol to convert the access path marked with the custom protocol into a resource path that complies with the custom protocol includes the following steps A1-A4:
[0111] A1: Call the URL parsing function in the callback processing function to parse the access path of the target resource and obtain the URL without the protocol name;
[0112] Exemplarily, the url parsing function is request.url.substr(), where Request is an object of information about the requested local resource, Request.url can be parsed to obtain the requested url address, and Substr is a Javascript method used to extract a part of a string.
[0113] For example, file: / / D:\Image\demo.png is the access path of a png image named demo in the Image folder of the terminal drive D. Call the URL parsing function request.url.substr(7) to extract the characters from the 7th character of file: / / D:\Image\demo.png to the last character, remove the protocol name, and get D:\Image\demo.png.
[0114] To facilitate subsequent processing, the URL without the protocol name is recorded as newurl.
[0115] A2: Call the normalization function in the callback processing function to normalize the URL and obtain a normalized URL;
[0116] Exemplarily, the normalization function is path.normalize(), which is a path operation function in Node.js. path.normalize(newurl) is used to normalize the newurl obtained by A1. path.normalize() performs the following operations:
[0117] A21: Remove the space characters at the beginning and end of newurl.
[0118] A22: Convert all slashes / and backslashes \\ to forward slashes / .
[0119] A23: Combine consecutive slashes / / into one slash / .
[0120] A24: Convert the current directory represented by . to / .
[0121] A25: Convert the parent directory represented by.. to.. / , and do nothing if the current directory is the root directory.
[0122] By normalizing newurl, the normalized URL can be correctly parsed and understood on different operating systems.
[0123] For subsequent processing convenience, the normalized URL is denoted as normal.
[0124] A3: Call the decoding function in the callback processing function to decode the normalized URL into a resource path;
[0125] Exemplarily, the decoding function is decodeURL(), and decodeURL() is a global function in JavaScript used to decode the URL encoded by the encodeURI() function. It can be understood that the access path of the target resource is the encoded URL. decodeURL() performs the following operations:
[0126] A31: Replace the encoded characters composed of the percent sign % and two hexadecimal digits in the URL string with the corresponding characters.
[0127] A32: Replace the + character in the URL string with a space character.
[0128] For example: The encoded URL is http: / / example.com / search?q=%E4%B8%AD%E5%9B%BD, and the decodeURI() function can be used for decoding:
[0129] const decodedURI = decodeURI('http: / / example.com / search?q=%E4%B8%AD%E5%9B%
[0130] BD');
[0131] console.log(decodedURI); / / http: / / example.com / search?q=中国。
[0132] By calling decodeURL(normal), correctly parse the Chinese name in normal to obtain the resource path denoted as zh.
[0133] A4: Call the callback function in the callback processing function to return the resource path.
[0134] Exemplarily, the callback function is callback(), and callback(zh) is used to return the resource path zh.
[0135] Exemplarily, the code for calling the callback processing function of the custom protocol to convert the access path marked with the custom protocol into a resource path that conforms to the custom protocol is as follows:
[0136] protocol.registerFileProtocol('atom', (request, callback) => {
[0137] / / Parse and intercept the requested url
[0138] const newUrl = request.url.substr(7)
[0139] / / Process slashes and other symbols for normalization
[0140] const normal = path.normalize(newUrl)
[0141] / / Decoding process
[0142] const zh = decodeURI(normal)
[0143] callback(zh)
[0144] })
[0145] This embodiment can bypass the restrictions of the same-origin policy by registering a custom protocol and achieve direct access to local resources, thereby providing developers with a more flexible resource management method.
[0146] A local resource loading method provided in an embodiment of the present application is introduced above, and a device for executing the above local resource loading method will be introduced below.
[0147] See also Figure 5 , Figure 5 A schematic diagram of the structure of a local resource loading device provided in an embodiment of the present application. Figure 5 As shown, the local resource loading device includes:
[0148] The judging unit 501 is used to judge, in response to a resource loading request of an Electron application, whether an access path of a target resource corresponding to the resource loading request is a local path;
[0149] A path processing unit 502, configured to add a custom protocol tag to the access path of the target resource if the access path of the target resource is a local path;
[0150] A path conversion unit 503 is used to call the callback processing function of the custom protocol to convert the access path marked with the custom protocol into a resource path that complies with the custom protocol;
[0151] The resource loading unit 504 is configured to load the target resource according to the resource path.
[0152] In a possible implementation, the local resource loading device further includes:
[0153] The protocol registration unit is used to register the custom protocol and set the name and callback processing function of the custom protocol when the Electron application is in the application ready state.
[0154] In a possible implementation, the path processing unit 502 is specifically configured to concatenate the name of the custom protocol and : / / before the access path of the target resource.
[0155] In a possible implementation, the path conversion unit 503 is specifically used to call the URL parsing function in the callback processing function to parse the access path of the target resource to obtain the URL without the protocol name; call the normalization function in the callback processing function to normalize the URL to obtain a normalized URL; call the decoding function in the callback processing function to decode the normalized URL into the resource path; call the callback function in the callback processing function to return the resource path.
[0156] In a possible implementation, the judgment unit 501 is specifically used to parse the resource loading request to obtain the access path of the target resource; and determine whether the access path of the target resource is a local path according to the protocol name in the access path of the target resource.
[0157] The present embodiment discloses a local resource loading device. If the access path of the target resource corresponding to the resource loading request is a local path, a custom protocol tag is added to the access path of the target resource, and a callback processing function of the custom protocol is called to convert the access path with the custom protocol tag into a resource path that complies with the custom protocol, so as to avoid the target resource being identified as a local resource and restricted from loading by the browser's homology policy, thereby bypassing the browser's homology policy restriction and realizing local resource loading in the Electron application.
[0158] The present application also provides an electronic device in an embodiment. Figure 6As shown, it shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiment of the present application. The electronic device in the embodiment of the present application may include but is not limited to a fixed terminal such as a laptop, a PAD (tablet computer), a desktop computer, etc. Figure 6 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0159] like Figure 6 As shown, the electronic device may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 to a random access memory (RAM) 603. When the electronic device is powered on, various programs and data required for the operation of the electronic device are also stored in the RAM 603. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0160] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a memory card, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 An electronic device having various devices is shown, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0161] An embodiment of the present application also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any local resource loading method provided in the embodiment of the present application.
[0162] A computer-readable storage medium is also provided in an embodiment of the present application. The storage medium carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any local resource loading method provided in an embodiment of the present application.
[0163] It should also be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.
[0164] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0165] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.
[0166] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.
Claims
1. A local resource loading method, characterized in that: include: In response to a resource loading request from an Electron application, determining whether an access path of a target resource corresponding to the resource loading request is a local path; If the access path of the target resource is a local path, adding a custom protocol tag to the access path of the target resource; Calling the callback processing function of the custom protocol to convert the access path marked with the custom protocol into a resource path that complies with the custom protocol; The target resource is loaded according to the resource path.
2. The local resource loading method according to claim 1, characterized in that: Before responding to the resource loading request of the Electron application, it also includes: When the Electron application is in the application ready state, the custom protocol is registered, and the name and callback processing function of the custom protocol are set.
3. The local resource loading method according to claim 1, characterized in that: The step of adding a custom protocol tag to the access path of the target resource includes: The name of the custom protocol and : / / are concatenated before the access path of the target resource.
4. The local resource loading method according to claim 1, characterized in that: The callback processing function of calling the custom protocol converts the access path marked with the custom protocol into a resource path that conforms to the custom protocol, including: Calling the URL parsing function in the callback processing function to parse the access path of the target resource to obtain the URL without the protocol name; Calling the normalization function in the callback processing function to normalize the URL to obtain a normalized URL; Calling the decoding function in the callback processing function to decode the normalized URL into the resource path; Calling the callback function in the callback processing function returns the resource path.
5. The local resource loading method according to claim 1, characterized in that: The determining whether the access path of the target resource corresponding to the resource loading request is a local path includes: Parsing the resource loading request to obtain an access path to the target resource; According to the protocol name in the access path of the target resource, it is determined whether the access path of the target resource is a local path.
6. A local resource loading device, characterized in that: include: A determination unit, configured to determine, in response to a resource loading request of an Electron application, whether an access path of a target resource corresponding to the resource loading request is a local path; A path processing unit, configured to add a custom protocol tag to the access path of the target resource if the access path of the target resource is a local path; A path conversion unit, used to call the callback processing function of the custom protocol, and convert the access path marked with the custom protocol into a resource path that conforms to the custom protocol; A resource loading unit is used to load the target resource according to the resource path.
7. The local resource loading device according to claim 6, characterized in that: Also includes: The protocol registration unit is used to register the custom protocol and set the name and callback processing function of the custom protocol when the Electron application is in the application ready state.
8. A computer program product, characterized in that The method comprises computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device implements the local resource loading method as claimed in any one of claims 1 to 5.
9. An electronic device, characterized in that: The method comprises at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the local resource loading method as described in any one of claims 1 to 5.
10. A computer storage medium, characterized in that: The storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the local resource loading method as described in any one of claims 1 to 5.
Citation Information
Cited By
Medical image display method and device based on multi-version browsers
CN116842286A
A method and device for displaying medical images based on a multi-version browser
CN116842286B
