Angular get html from url
I'm trying to get some url html content from angular But all I get is Cross-Origin Request Blocked. Here's the code I was trying to use.
I expect to find in the console
asked Apr 24, 2019 at 14:25
0 try this:
or install extension answered Apr 24, 2019 at 15:53
1 I thought I would try to share a small solution for a funny problem with you (notice how I share it personally with ‘you’ — If you are here I welcome you and am pleased to be able to help — If you are not here no one will ever notice that I used the term ‘you’. Clever huh ;) The other day we had to implement new terms in our on-boarding form. The catch was that the terms were hosted on an external website. Our site is built in Angular 11 and this seems like a trivial issue that could be done in a matter of seconds. Which it can if you omit the UX part. The first suggestion was merely an a-element pointing to the URL with the terms:
The main problem with this is that your users are now thrown to an external website in the middle of the on-boarding process. Furthermore there is no easy way back; you cannot check ‘Accept’ on the external page, you have to close the page again manually and get back to the form and so forth. Just not good UX. The solution — Web scraping Why not scrape the terms text from the external website and insert it in a modal instead? Let’s do that. First we need to know where and what we are trying to get. The ‘where’ is just the URL for the website. We will use async/await (that’s ES7 I believe) and first get the entire web page HTML: (async () => { The console will then print out all the HTML — But with a catch: This is a string! For the ‘what’ go to said website, right click the text and choose ‘Inspect element’. This is what it looked like in my case: The DIV marked in blue is what I need. The closest I get to something I can directly address is the sibling DIV with the ID=”sidebar” so let’s try that. Our challenge here is that we cannot navigate the HTML in string format — We need a DOM so let’s format the string ‘back’ into HTML. I chose a well used and maintained npm package (async () => {
htmlTerms: string;(async () => { First we took out all the HTML as a string with response.text() but in order to get to the juicy parts we parsed it back to HTML with parse(text) and traversed this newly created DOM with Note that Angular has built in XSS and that you might need more manipulation than shown above. Also you will loose the HTML if the external webpage is changed related to your scraping code. I chose an interactive modal for the terms (in German ;) but you can implement it anyway you want. You can of course manipulate the code or add your own styling (or their styling) but the more you manipulate the more can break if the original website is changed so I prefer KISS. Have fun. |