B
B
Bogdan Borovkov2015-08-12 23:01:51
JavaScript
Bogdan Borovkov, 2015-08-12 23:01:51

How to add jquery to head html script from c# application?

Good afternoon!

It is necessary that the jquery library is connected to any downloaded site.

Here is part of the code:
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
this.webKitBrowser1.Navigated +=
new WebBrowserNavigatedEventHandler(webKitBrowser1_Navigated);

this.webKitBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted);
}
void webKitBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebKit.DOM.Document doc = webKitBrowser1.Document;
WebKit.DOM.Node head = doc.GetElementsByTagName("head")[0];
WebKit.DOM.Element jQuery = doc.CreateElement("script");
jQuery.SetAttribute("src", " ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery... ");
head.AppendChild(jQuery);
}

Question: what's wrong? Here, it doesn't work. I specifically load a page that requires jquery to work, but it doesn't work. I read the webkit manual: everything seems to be correct.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Bogdan Borovkov, 2015-08-19
@Bonifats

browser.StringByEvaluatingJavaScriptFromString("(function(doc){ var head = doc.getElementsByTagName('head')[0]; var script = doc.createElement('script'); script.setAttribute('src', ' code.jquery .com/jquery-latest.min.js); head.appendChild(script);})(document);");
Here is the result of the research of a familiar, much more experienced programmer. And then only after I changed WebKit.NET to the openwebkitsharp assembly.

V
Vitaly Pukhov, 2015-08-13
@Neuroware

Perhaps you need to load a little earlier than "DocumentCompleted", because by the time it happens, other scripts that needed jquery are probably crashing, but this is purely guesswork, because not knowing why this was needed is difficult to answer. Alternatively, you can load the page sorts, inject the script import in the right place and display already "modified" sorts.

D
Dmitry Makarov, 2015-08-13
@DmitryITWorksMakarov

First of all, you need to try to pull the Refresh or Update methods from webKitBrowser, although most likely this should not bring results, because most likely WebKit renders somewhere in its buffer and simply updates the screen from the buffer using these methods.
And why do you need such functionality in the browser? If the page uses jQuery, then please add it to the page. If not used, then it is not needed. Or is it meant some other script to the heap / or instead of loading? Sniffer there or what else? =)
In theory, loading and displaying the page in the browser goes, if simplified, like this: loading-> building DOM-> rendering. Moreover, modern browsers do this in a stream, that is, gradually displaying what is loaded, but in some cases they can re-render everything (for example, when the window is resized). In your case, you need to wedge somewhere in the process of loading the header or adding a header to the DOM, but I didn’t see any hooks / events in WebKit.NET for this possibility.
You can try to make a fake scaling or artificially trigger a window resize event, but this is a dirty hack.
Well, something similar to the truth can be done if you implement a certain proxy through which to send http traffic, look for the header on the fly and insert your script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question