C++Language FeatureLearn C++

Learn To Use Script Injection Method In Modern C++ On Windows With EdgeBrowser

teal white and pink paint

If you want to analyze browsed web pages, first you need a web browser component. We can use Script Injection methods to get more details about the HTML or XML documents, for example to get Title of that webpage, or full Body of that page, or value of an element. This is very useful for data mining or for analyzing web pages, this can be applied with time intervals for refreshing web pages, like pages about weathers, news etc.

Document Object Model (DOM)

The Document Object Model (DOM) represents an HTML or XML document in memory and it is the data representation of the objects that comprise the structure and content of a document on the web. So you can use APIs to edit or create web content and applications. DOM is described well on mozilla.org here.

For example for HTML and XML documents;
DOM Object Document properties can be found here https://developer.mozilla.org/en-US/docs/Web/API/Document
DOM Object Body properties can be found here https://developer.mozilla.org/en-US/docs/Web/API/Document/body

How HTML XML Injection is Performed

DOM document has properties and iinnerHTML is the property of DOM document and with innerHTML, we can write dynamic HTML code. It is used mostly for data input fields like comment fields, questionnaire forms, registration forms, etc.

Script Injection Method in Modern C++

In RAD Studio, C++ Builder 10.4 and above there is a EdgeBrowser (TEdgeBrowser) component to navigate on web pages and it wraps the latest Microsoft WebView2 component. The EdgeBrowser component is easy to use to navigate on web pages than the previously WebBrowser which has some incompatibilities on pages. This new web browser supports fully WebView dynamic library of Canary version. If you are new to EdgeBrowser please read this and Learn To Develop Your Web Browser App in C++ Builder on Windows (EdgeBrowser).

With these information above, if you are able to successfully run EdgeBrowser as in examples; now we can easily add Script Injection method to retrieve the full text of document. This method fully gets all document data as in UnicodeString which means it can be used to analyze web pages in any language.

In C++ Builder, EdgeBrowser has Script Injection method. To apply this we should use this line as below;

or you can directly navigate from Text of your Edit as here;

For example you can obtain full body of document with this Script execution;

Here we are injecting to ask this.document property and DOM object will return this request. To obtain this request, go to properties of EdgeBrowser, double click to ExecuteScript event and add this code to copy content of the results from web page to Memo

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?

Worth reading...
Introduction To C++ Windows Development With C++Builder