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].

Hash
  • 4,647
  • 5
  • 21
  • 39
fazil tm
  • 299
  • 1
  • 5
  • 23

4 Answers4

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).

0

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

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.

lawstud
  • 185
  • 14
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);
vusan
  • 5,221
  • 4
  • 46
  • 81
  • 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":""}} – fazil tm Aug 23 '16 at 09:53
  • using comma is not possible. – fazil tm Aug 23 '16 at 09:55
  • 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. – vusan Aug 23 '16 at 11:49
  • Here is the source code https://github.com/ncalexan/geckobrowser-gradle pls help me – fazil tm Aug 24 '16 at 03:41
  • I want to highlight phone numbers like skype "click to call" service. How it possible using this code. pls help me – fazil tm Aug 24 '16 at 04:03