1

I am developing a customized gecko powered android browser. I want to print the source code in console.

When I try to print it shows [object HTMLDocument].

The code is given below :

 function onPageLoad(event) {
   // the target is an HTMLDocument
   let contentDocument = event.target;
 let browser = BrowserApp.getBrowserForDocument(contentDocument);
   console.log("Page loaded: " + browser.contentTitle);
   console.log("Page loaded content: " + browser.contentDocument);

 }

The output is Page loaded content: [object HTMLDocument]

I want to print the source code in [object HTMLDocument].

4 Answers 4

2

Ah, I see. Try:

let contentDocument = event.target;
console.log("Page loaded: " + contentDocument.title);
var s = new XMLSerializer().serializeToString(contentDocument);
console.log("Page loaded content: " + s);

This worked for me at least (if I understand correctly what you want to print that is).

Sign up to request clarification or add additional context in comments.

4 Comments

The output shows "Page loaded content: undefined". The codes are written in javascript file.
which is the import file for XMLSerializer in android studio. I have got an error is JavaScript Error: "ReferenceError: XMLSerializer is not defined"
I think it is "import org.xmlpull.v1.XmlSerializer;", hope it works!
It helped me with a JavaScript import, so thank a lot!
0

Have you tried converting it to a String? For example, console.log("Page loaded: " + String(browser.contentTitle));

1 Comment

The result is shown as "Page loaded: [object HTMLDocument]". I want to print the source code.
0

Try this:

HTMLEditorKit tmp = new HTMLEditorKit(); 
HTMLDocument doc = (HTMLDocument) tmp.createDefaultDocument(); 
StringWriter writer = new StringWriter();
tmp.write(writer, doc, 0, doc.getLength());
String s = writer.toString();
console.log(s);

I hope it will help.

3 Comments

Not getting the result. s is not printing.
ok, so try checking here: stackoverflow.com/questions/12035316/… - this StringBuilder answer worked for me once :)
Not getting any results.
0

Put , instead of + in console.log() function as console.log() also support object. Just you need to separate by comma.

console.log("Page loaded: " , browser.contentTitle);
console.log("Page loaded content: " , browser.contentDocument);

5 Comments

The output is I/Gecko: console.log: Page loaded: Asset Test I/Gecko: console.log: Page loaded content: HTMLDocument {"location":{"href":"resource://android/assets/test.html","origin":"resource://android","protocol":"resource:","username":"","password":"","host":"android","hostname":"android","port":"","pathname":"/assets/test.html","search":"","hash":""}}
using comma is not possible.
It's just showing what it contains. browser.contentTitle and browser.contentDocument do not contains any source code. I guess event.target would give you source code.
Here is the source code github.com/ncalexan/geckobrowser-gradle pls help me
I want to highlight phone numbers like skype "click to call" service. How it possible using this code. pls help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.