[英]How do I get the link attributes (page name) in the “Webbrowser” event when I click a link?
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. HTML格式的文本存储在数据库中。
The category tree displays text from the database to the Web browser. 类别树显示从数据库到Web浏览器的文本。
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: 也就是说,从页面“ Page_1”到页面“ Page_2”逻辑的必要段落的转换:
- User. -用户。 It is on page "Page_1".
它在页面“ Page_1”上。
- User. -用户。 Click the link on the page "Page_1".
单击页面“ Page_1”上的链接。
- Browser. -浏览器。 Opens the page "Page_2", scrolls to "Paragraph_N" ("N" depends on which paragraph on the page is "Page_1")
打开页面“ Page_2”,滚动到“ Paragraph_N”(“ N”取决于页面上的哪个段落是“ Page_1”)
The transition between the pages I plan to do with links "anchor" 我计划使用链接“ 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; -从链接中提取链接所引用页面的名称(“ text.html”);
- extract the page from the database according to the name; -根据名称从数据库中提取页面;
Example HTML code "Page1" and "Page 2" 示例HTML代码“ Page1”和“ Page 2”
https://codeshare.io/2Bb03L https://codeshare.io/2Bb03L
Questions 问题
1. How do I get the link attributes (page name) in the "Webbrowser" event when I click a link? 1.单击链接后,如何在“ Webbrowser”事件中获取链接属性(页面名称)?
2. What are the more rational ways to perform this logic (go from page "Page_1" to page "Page_2")? 2.执行此逻辑的更合理的方法是什么(从页面“ Page_1”到页面“ Page_2”)?
3.0 How to make the following actions when the link is clicked: 3.0单击链接时如何进行以下操作:
3.1. 3.1。 If the link to the site - open in Chrome?
如果链接到网站-在Chrome中打开?
3.2. 3.2。 If the link to the page from the database - open in "webBrowser1"?
如果从数据库链接到页面-在“ webBrowser1”中打开?
3.3. 3.3。 If the link to the paragraph of pages from the database - open in
如果链接到数据库中页面的段落-在
"webBrowser1" and scroll the scroll to the desired paragraph? “ webBrowser1”并将滚动条滚动到所需的段落?
image 图片
Answering your first question " How do I get the link attributes (page name) in the "Webbrowser" event when I click a link? ": 回答您的第一个问题“ 单击链接时如何在“ Webbrowser”事件中获取链接属性(页面名称)? ”:
Assuming your Webbrowser is called webBrowser1
: 假设您的Webbrowser名为
webBrowser1
:
You need expose a C# object to the WebBrowser that the JavaScript can call directly, the object must have the ComVisibleAttribute set true. 您需要将C#对象暴露给JavaScript可以直接调用的WebBrowser,该对象必须将ComVisibleAttribute设置为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: 配置您的Webbrowser:
webBrowser1.ObjectForScripting = new ScriptInterface();
Now in your js you can call the OnAnchorClick method this way: 现在,在您的js中,您可以通过以下方式调用OnAnchorClick方法:
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 如何通过webbrowser控件为winforms处理javascript事件
how-to-inject-javascript-in-webbrowser-control 如何在浏览器控件中注入JavaScript
calling-javascript-in-a-webbrowser-control-from-c# 从C调用Web浏览器控件中的JavaScript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.