Skip to content

Commit

Permalink
HTTP server: add experimental DOM-dump mode
Browse files Browse the repository at this point in the history
Simply dumps the contents of the DOM
  • Loading branch information
tombh committed Jun 19, 2019
1 parent 81f41b7 commit d6b5951
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions interfacer/src/browsh/raw_text_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func isProductionHTTP(r *http.Request) bool {

// 'PLAIN' mode returns raw text without any HTML whatsoever.
// 'HTML' mode returns some basic HTML tags for things like anchor links.
// 'DOM' mode returns a simple dump of the DOM.
func getRawTextMode(r *http.Request) string {
var mode = "HTML"
if strings.Contains(r.Host, "text.") {
Expand All @@ -233,6 +234,9 @@ func getRawTextMode(r *http.Request) string {
if r.Header.Get("X-Browsh-Raw-Mode") == "PLAIN" {
mode = "PLAIN"
}
if r.Header.Get("X-Browsh-Raw-Mode") == "DOM" {
mode = "DOM"
}
return mode
}

Expand Down
2 changes: 1 addition & 1 deletion interfacer/src/browsh/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package browsh

var browshVersion = "1.6.1"
var browshVersion = "1.6.2"
6 changes: 6 additions & 0 deletions interfacer/test/http-server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ var _ = Describe("HTTP Server", func() {
"<a href=\"/http://localhost:4444/smorgasbord/another.html\">Another page</a>"))
})

It("should return the DOM", func() {
response := getPath("/smorgasbord", "dom")
Expect(response).To(ContainSubstring(
"<div class=\"big_middle\">"))
})

It("should return a background image", func() {
response := getPath("/smorgasbord", "html")
Expect(response).To(ContainSubstring("background-image: url(data:image/jpeg"))
Expand Down
3 changes: 3 additions & 0 deletions interfacer/test/http-server/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func getPath(path string, mode string) string {
if mode == "plain" {
request.Header.Add("X-Browsh-Raw-Mode", "PLAIN")
}
if mode == "dom" {
request.Header.Add("X-Browsh-Raw-Mode", "DOM")
}
response, err := client.Do(request)
if err != nil {
panic(fmt.Sprintf("%s", err))
Expand Down
2 changes: 1 addition & 1 deletion webext/src/dom/commands_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default MixinBase =>

_launch() {
const mode = this.config.http_server_mode_type;
if (mode === "raw_text_plain" || mode === "raw_text_html") {
if (mode.includes("raw_text_")) {
this._is_raw_text_mode = true;
this._is_interactive_mode = false;
this._raw_mode_type = mode;
Expand Down
8 changes: 7 additions & 1 deletion webext/src/dom/serialise_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,14 @@ export default MixinBase =>
}

_sendRawText() {
let body;
if (this._raw_mode_type == "raw_text_dom") {
body = document.getElementsByTagName("body")[0].innerHTML;
} else {
body = this._serialiseRawText();
}
let payload = {
body: this._serialiseRawText(),
body: body,
page_load_duration: this.config.page_load_duration,
parsing_duration: this._parsing_duration
};
Expand Down
8 changes: 7 additions & 1 deletion webext/src/dom/text_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export default class extends utils.mixins(CommonMixin, SerialiseMixin) {
sendRawText(type) {
this._raw_mode_type = type;
this._parse_start_time = performance.now();
this.buildFormattedText(this._sendRawText.bind(this));
if (type == "raw_text_dom") {
setTimeout(() => {
this._sendRawText();
}, this.config["http-server"].render_delay);
} else {
this.buildFormattedText(this._sendRawText.bind(this));
}
}

buildFormattedText(callback) {
Expand Down

0 comments on commit d6b5951

Please sign in to comment.