0

I plan to make a content management system.
System composition:
- The page tree by category. (Page tree by category);
- Web browser. (webbrowser);

Contents of the pages:
- formatted text;
- Pictures;
- references;

Text in HTML format is stored in the database.
The category tree displays text from the database to the Web browser.

It is necessary to make a transition between pages.

That is, the transition from page "Page_1" to the necessary paragraph of the page "Page_2" Logics:
   - User. It is on page "Page_1".
   - User. Click the link on the page "Page_1".
   - Browser. Opens the page "Page_2", scrolls to "Paragraph_N" ("N" depends on which paragraph on the page is "Page_1")

The transition between the pages I plan to do with links "anchor"

<a href="text.html#bottom"> Go to the bottom of the text </a>  

I think that to perform this logic, need the application to do the following:
   - track the event "clicking on the link";
   - Extract from the link the name of the page ("text.html") referenced by the link;
   - extract the page from the database according to the name;

Example HTML code "Page1" and "Page 2"
https://codeshare.io/2Bb03L

Questions
1. How do I get the link attributes (page name) in the "Webbrowser" event when I click a link?
2. What are the more rational ways to perform this logic (go from page "Page_1" to page "Page_2")?
3.0 How to make the following actions when the link is clicked:
3.1. If the link to the site - open in Chrome?
3.2. If the link to the page from the database - open in "webBrowser1"?
3.3. If the link to the paragraph of pages from the database - open in
"webBrowser1" and scroll the scroll to the desired paragraph?
image

  • See the [WebBrowser.Navigating](https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating(v=vs.110).aspx) event. The `WebBrowserNavigatingEventArgs` gives you the URL to which the Browser is going to and the `e.Cancel` property allows you to interrupt the action. Use the URL to get the page from your source and pass it to the WebBrowser. The Navigating event should contain some logic to understand when your code is raising the event, like `bool ItsMe = true`. The second question implies that there's a somewhat irrational way to do this. – Jimi May 11 '18 at 16:34
  • @Jimi Updated the question. I do not understand. I'm a beginner I do not plan to use a web server. I place "URL" only the name of the page. How to make the following actions when the link is clicked: 1. If the link to the site - open in Chrome. 2. If the link to the page from the database - open in "webBrowser1" 3. If the link to the paragraph of pages from the database - open in "webBrowser1" and scroll the scroll to the desired paragraph. Example HTML code "Page1" and "Page 2" https://codeshare.io/2Bb03L Could you give an example for this situation? – user9776818 May 11 '18 at 17:08
  • I never mentioned a Web Server. `Href` tag has it's own syntax, you need to comply. [Study it a little](https://www.w3schools.com/html/). Follow the link to `WebBrowser.Navigating`, see how it works, search Google and SO for examples. You'll find many. When you have a specific coding problem, post a specific question (Btw, this one you already have posted). – Jimi May 11 '18 at 17:43
  • Please take a look at [ASK]. Don't ask multiple questions in a single post. – Reza Aghaei May 11 '18 at 19:46

1 Answers1

0

Answering your first question "How do I get the link attributes (page name) in the "Webbrowser" event when I click a link?":

Assuming your Webbrowser is called webBrowser1:

You need expose a C# object to the WebBrowser that the JavaScript can call directly, the object must have the ComVisibleAttribute set true.

[System.Runtime.InteropServices.ComVisible(true)]
public class ScriptInterface
{
    public void OnAnchorClick(string href)
    {
        var parts = href.Split('#');
        var pageName = parts[0];
        //do whatever with page name    
    }
}

Configure your Webbrowser:

webBrowser1.ObjectForScripting = new ScriptInterface();

Now in your js you can call the OnAnchorClick method this way:

document.getElementsByTagName('a')[0].onclick = function(){
   window.external.OnAnchorClick(this.href);
   return false;
}

Other usefull references:

how-to-handle-javascript-events-via-webbrowser-control-for-winforms

how-to-inject-javascript-in-webbrowser-control

calling-javascript-in-a-webbrowser-control-from-c#

ernesthm
  • 421
  • 4
  • 15