i am trying to get html string from my site as it presented in browser
firstly i tried to use web client
using (var client = new WebClient())
{
var content = client.DownloadString("my_site_address");
}
but in my site i have some javascript code that change the view (and webClient does not run javascript)
so i use wpf WebBrowser and after nevigate to the desire site it show the page (as expected) but when i try to get the html string it show just like the webClient
dynamic doc = MainBrowser.Document;
var htmlText = doc.documentElement.InnerHtml;
this is how i get the html:
<!DOCTYPE html>
<head>
<title>Title</title>
</head>
<body>
<div class="conteiner">
<div class="matrix">
<script type="text/javascript">
// some script code
</script>
<script type="text/javascript" src="xxx"></script>
Matrix
</div>
<div class="zoom">
Zoom
</div>
</div>
<div class="test">
<script type="text/javascript">
// some script code
</script>
<script type="text/javascript" src"xxx2"></script>
</div>
</body>
</html>
and this is how i should get it after the javascript change it:
<html><head>
<title>Title</title>
</head>
<body>
<div class="conteiner">
<div class="matrix">
<script type="text/javascript">
</script>
<script type="text/javascript" src="xxx"></script><iframe ></iframe><script ></script><div ><div ><iframe >
<html><head>
<title></title>
</head>
<body>
<div >
<ul><li><ol><li <a </a></li></ol></li></ul> </div>
</body></html>
</iframe></div></div></div>
Matrix
</div>
<div class="zoom">
Zoom
</div>
</div>
<div class="test">
<script type="text/javascript">
</script>
<script type="text/javascript" src="xxx2"></script><div ><div ><div ><iframe ></iframe></div></div></div>
</div>
</body></html>
Please help :)
You can use the WebDriver framework from Selenium. It offers different web driver implementations, like for Internet Explorer or Firefox.
Here is some sample code to request a web site with Internet Explorer, let it render and finally save the final HTML markup.
public class WebSiteHtmlLoader : IDisposable
{
private readonly RemoteWebDriver _remoteWebDriver;
public WebSiteHtmlLoader(RemoteWebDriver remoteWebDriver)
{
if (remoteWebDriver == null) throw new ArgumentNullException("remoteWebDriver");
_remoteWebDriver = remoteWebDriver;
}
public string GetRenderedHtml(Uri webSiteUri)
{
if (webSiteUri == null) throw new ArgumentNullException("webSiteUri");
_remoteWebDriver.Navigate().GoToUrl(webSiteUri);
return _remoteWebDriver.PageSource;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (_remoteWebDriver != null)
{
_remoteWebDriver.Quit();
}
}
}
}
Usage:
class Program
{
static void Main(string[] args)
{
if (!args.Any())
{
return;
}
var pageUrl = args.First();
var options = new InternetExplorerOptions
{
IntroduceInstabilityByIgnoringProtectedModeSettings = true,
PageLoadStrategy = InternetExplorerPageLoadStrategy.Eager
};
using (var htmlLoader = new WebSiteHtmlLoader(new InternetExplorerDriver(options)))
{
var html = htmlLoader.GetRenderedHtml(new Uri(pageUrl, UriKind.Absolute));
File.WriteAllText(#"C:\htmlloadertext.html", html);
}
}
}
You could try to use the WebBrowser.DocumentText property. Like, add a hidden WebBrowser control to your application and call Navigate() function, then call the property to get the generated HTML
More info at: http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documenttext.aspx
Related
I am able to call java from javascript on click of Button. But the same function is not getting invoked on document.ready
Here is my code
This is the index.html inside asset
<!DOCTYPE >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function init()
{
var testVal = 'Привет от Android Tools!';
AndroidFunction.showToast(testVal);
}
$(document).ready(function() {
//$('form').submit();
init();
});
</script>
</head>
<body>
<div style="clear: both;height: 3px;"> </div>
<div>
<input value="submit" type="button" name="submit"
id="btnSubmit" onclick="javascript:return init();" />
</div>
</body>
</html>
This is the kotlin code
class JavaScriptInterface internal constructor(c: Context) {
var mContext: Context
#JavascriptInterface
fun showToast(toast: String?) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show()
}
private fun callDummyPage(webView : WebView){
val webSettings: WebSettings = webView.getSettings()
webSettings.javaScriptEnabled = true
webView.webViewClient = MyWebViewClient()
webView.webChromeClient = MyWebChromeClient()
webView.addJavascriptInterface(JavaScriptInterface(activity!!.baseContext), "AndroidFunction");
webView.loadUrl("file:///android_asset/index.html");
}
init {
mContext = c
}
}
ready() is a jQuery method and you are not importing the library. You can try importing the library at your scripts adding <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> on your <head> ... </head>
You can also change your function to plain Vanilla Js
document.addEventListener("DOMContentLoaded", function(event) {
// Your code to run since DOM is loaded AND ready
});
I have loaded local html using Android WebView renderer. The html is loading properly. But the scripts which is referred inside html file is not loaded. The script files are located inside the assets folder.
Render File Code
protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var webView = new Android.Webkit.WebView(Forms.Context);
webView.Settings.JavaScriptEnabled = true;
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.JellyBean)
{
webView.Settings.AllowUniversalAccessFromFileURLs = true;
webView.Settings.AllowFileAccessFromFileURLs = true;
}
SetNativeControl(webView);
}
if (e.NewElement != null)
{
Control.LoadUrl("file:///android_asset/Sample/index.html");
}
}
Html Code
<!DOCTYPE html>
<html class="no-js">
<head>
<script src="file:///android_asset/jquery-1.10.2.min.js"></script>
<!-- Render -->
<script src="file:///android_asset/epub.min.js"></script>
<script type="text/javascript">
var book = ePub("file:///android_asset/Sample/Azure_Cosmos_DB_and_DocumentDB_Succinctly/);
$(document).ready(function () {
book.renderTo("area");
});
</script>
</head>
<body>
<div id="main">
<div id="prev" onclick="book.prevPage()" style="font-size: 64px;cursor:pointer;" class="arrow">‹</div>
<div id="area" ></div>
<div id="next" onclick="book.nextPage()" style="font-size: 64px;cursor:pointer;" class="arrow">›</div>
</div>
</body>
</html>
Can anyone suggest me to load corresponding JS files in webview rendering method?
Normally you would place your script tag at the bottom of your HTML not in the top.
<!DOCTYPE html>
<html class="no-js">
<head>
</head>
<body>
<div id="main">
<div id="prev" onclick="book.prevPage()" style="font-size: 64px;cursor:pointer;" class="arrow">‹</div>
<div id="area" ></div>
<div id="next" onclick="book.nextPage()" style="font-size: 64px;cursor:pointer;" class="arrow">›</div>
</div>
</body>
<script src="file:///android_asset/jquery-1.10.2.min.js"></script>
<!-- Render -->
<script src="file:///android_asset/epub.min.js"></script>
<script type="text/javascript">
var book = ePub("file:///android_asset/Sample/Azure_Cosmos_DB_and_DocumentDB_Succinctly/");
$(document).ready(function () {
book.renderTo("area");
});
</script>
</html>
You have also forgot a " at the end of var book = ePub("file:///android_asset/Sample/Azure_Cosmos_DB_and_DocumentDB_Succinctly/");
I'm trying to run a simple example of signalr, the example that i'm trying to do is here.
I verified that $.connection.hub.start().done is working fine. The problem is chat.client.broadcastMessage is not executed as i put an alert inside it to make sure but that alert did not execute. Can any one help me to fix my problem? Here is my code:
Chatting page.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="scripts/jquery-1.6.4.min.js"></script>
<!--Reference the SignalR library. -->
<script src="scripts/jquery.signalR-2.2.1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script> <!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
</body>
</html>
ChatHub.cs
public class ChatHub : Hub
{
//used to send message to all clients
public void send(String name, String message)
{
Clients.All.brodcastMessage(name,message);
}
}
Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
NOTE: I've figured out that, when i type chat. the dropdown menu does not show me client thus i can not call chat.client.broadcastMessage, why?
I solved the problem by using addMessage instead of brodcastMessage and it works.
here is the code...
chat.client.addMessage = function (name, message) //script side
Clients.All.addMessage(name,message); //inside send method in the hub class
Here is the the html code:
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body class="- view-form">
<div id="container"> </div>
<script type="text/javascript">
var sub_chart_path = "device";
var is_dependency = false;
var max_zoom_factor = 5;
var image_name_base = "NHCTCC01";
</script>
</body>
</html>
What I want is to locate the element inside the <script type="text/javascript">. Can anybody tell me how it can be located?
You need to use the execute_script with Javascript executor and get the variable in return
driver.execute_script("return sub_chart_path;")
Should print:
device
I'm working on a Chrome extension where I need to pass highlighted text into a browser_action. I found the following code in a Google Group, and at the time it was written it was still valid - but it doesn't work anymore..
Does anyone know an alternative solution?
background.html:
<html>
<head>
<script type="text/javascript">
var selection_callbacks = [];
function getSelection(callback) {
selection_callbacks.push(callback);
chrome.tabs.executeScript(null, { file: "contentscript.js" });
};
chrome.extension.onRequest.addListener(function (request) {
var callback = selection_callbacks.shift();
callback(request);
});
</script>
</head>
<body>
</body>
</html>
popup.html:
<html>
<head>
<script type="text/javascript">
function onSelection(text) {
document.getElementById("output").innerHTML = text;
}
chrome.extension.getBackgroundPage().getSelection(onSelection);
</script>
</head>
<body>
<div id="output">
This should be replaced with the selected text
</div>
</body>
</html>
contentscript.js:
chrome.extension.sendRequest(window.getSelection().toString());
You could use a real content script instead of injecting JavaScript into the page with chrome.extension.executeScript. You could then have background.html ask the content script for the selection using chrome.tabs.sendRequest.