1

I'm working on a WindowsForm based RSS Aggregator as a hobby project. I'm running into an issue while displaying descriptions from RSS feeds that contain html in a webbrowser control.

An example of a description that causes me a problem:

"<img border=\"0\" src=\"//images10.newegg.com/NeweggImage/ProductImageCompressAll125/14-487-346-Z01.jpg\" width=\"125\" height=\"94\" title=\"\" alt=\"\">

The problem is the img src. If I add http: in front of it, it loads the image just fine. Otherwise, I get an x box, and not the kind you can play games on unfortunately.

A work around would be to parse every description and add the http: string everywhere that is necessary, but that seems like a lot of unnecessary coding and every feed would be different.

Is there a better way to show RSS Feed data that's formatted with html, or is there a way to fix the issue described above with the webbrowser control?

Tyler Sells
  • 463
  • 4
  • 16
  • Typically, when a source file begins with `//`it means that the particular file can be served over `http` or `https`. When parsed, the current protocol is appended normally by the webbrowser. As an example when you are on a secure page `https://www.example.com` that resource would be read as `https://images10.newegg.com/NeweggImage/ProductImageCompressAll125/14-487-346-Z01.jpg` whereas on a non-secure page `http://www.example.com` it would be read as `http://images10.newegg.com/NeweggImage/ProductImageCompressAll125/14-487-346-Z01.jpg` – Darnell W. Sep 05 '17 at 05:02

2 Answers2

3

How to show the RSS Feed data that's formatted with HTML?

I'm not sure if there is a way.


Is there a way to fix the issue described above with the webbrowser control?

You can easily parse all the img's src attributes prepending http: using jquery:

$(function() {
    $('img').attr('src', function(index, src) {
       return 'http:' + this.getAttribute('src');
    });
});
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I don't like this answer because you basically repeat what is stated int he question, they already prepend with `http` they are looking for a better way to do this. Though I will not downvote it because it may be all some people need. – Darnell W. Sep 05 '17 at 05:10
  • Dear @ImaBrokeDude, this isn't a chat site. We're a bunch of professional programmers. Note how I quoted OP to answer multiple Questions inline. And OP asks for solution. Please double read next time before commenting. Thank you. – Jeremy Thompson Sep 05 '17 at 11:10
  • Hello, I do not treat this site like a chat site. You may not like the name I have chosen, but what I stated was valid. Why repeat exactly the same thing the OP already asked? Specifically about prepending `http`. You can clearly see they have done this already. On top of the your answer steps outside of the language specified for the given question, why should the user be using javascript to parse a C# string? – Darnell W. Sep 05 '17 at 13:39
  • @Jeremy Thompson: Thank you for your answer. The reason I wanted to avoid parsing the links was because all RSS feeds could be different in terms of how they write their links. However, if this is the only way to solve the issue, it must be done. – Tyler Sells Sep 05 '17 at 13:43
  • 1
    @ImaBrokeDude: I'm remaining neutral in this, but for the sake of clarity and learning, there is validity to his use of jQuery. jQuery, while not directly related to c# and WinForms, is built for parsing HTML elements while c# HTML parsing would probably require some extra work. It would follow reason that I would include that jQuery in the document body of the WebBrowser control in order to parse the html elements on the fly without having to write more complex c# parsers to modify the data before it gets passed to the WebBrowser control. Is that correct Jeremy Thompson? – Tyler Sells Sep 05 '17 at 13:51
  • 1
    Fair enough, I can understand that. But yes, whenever you want a resource to load over the same protocol as the webpage, you can remove the protocol from the url so that the browser is to parse the resource link and append the correct protocol. Just wanted to be sure to let you know to be sensitive to whether the RSS feed is coming over `http` or `https`. See [RFC 3986: "Uniform Resource Identifier (URI): Generic Syntax", Section 4.2.](http://www.ietf.org/rfc/rfc3986.txt) – Darnell W. Sep 05 '17 at 14:10
1

Since the URL in question is not prepended with a protocol, it will be considered a relative URL, but there is obviously not going to be a local resource to display. More on the subject

You can mitigate this by using the solution described in Determine if Absolute or Relative URL coupled with an attempt to stick http: on the source src in case of failure, and retry.

Mihai Pantea
  • 360
  • 3
  • 8