prerender_rails_embedded 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzU2Y2U3YWRmMTViMjMxMzFlMzc1ZmMzMGQ3NWVjN2Q5ZTllMjcxNw==
4
+ MmQ5YjlhZGVlNThiOTcwZDczNzNlMTRiNmEwMzA1YWFkOTQxNjMzMA==
5
5
  data.tar.gz: !binary |-
6
- NzE3NGZmOWQxMGUwMWYxMDAwMmU2ZTViMzk4ZmQwZjc4MGM4OTQyZA==
6
+ NTFlYzNmOGM2Mzc0MmQxMmM3OTM3NzdmYTAwZDk4NTMwZmMwNmJkNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWE3ZTU2NGU0NjcxOWQyMmM2MTBlN2FkYmFkNmNmZDYwMmU3MjQ1ODAyMWYx
10
- ZDBlN2QwMmVhYWUyNjU3OWYyZTc5MDhlOTdhZGQwMTg0NzNkMjc2YTdkZDY0
11
- MjE2M2QyYTJhOGQyNDQ1N2YyNGNmMWI0MWUwMTA0MjE4NDY0NTM=
9
+ YmEyNmE3ZWE3ZWQ4MWM2NTgzN2VmZjljY2IwNjRhNWUyOWUxNGMzMmVhYzZl
10
+ MTNiMmI2YjE3Zjc5NjE0YmU4MTBjYzAyODM5ZWYyN2Y2NGU5YWNjOTc5ZDY4
11
+ ZDJlODExMDMyMThmZDY1OTQ2MDEwMTc2YWQ0YmI4ODkyYmU5NTY=
12
12
  data.tar.gz: !binary |-
13
- ZTEzZDExYjAzMDc0NTIxYzZkM2U4MGU5OGYzYjY1MzQ2YzJiN2YyYjBjM2Ix
14
- ZmE3MzNlODVkZWM2NmQwZDgyMDA0MDU1MDk2OTU3YWI3YTZkYmQxNjFkOTk4
15
- MjI4YTUzMDkzYTVjMjM5NzMzNjI0MmU0ZjRkYjNmNTBjNTU5OWU=
13
+ YzA3ZTljYWNjNTRhODg4ZDE5MDU1MDc0MWM0NjYyMTllMTgxY2E1MjUwZDBk
14
+ ZjdhZjE4ZDNmMTgyZGVlMTQyOTI2NjEyYzEyYThjYzlhYWVhN2ZkYjU2NWJk
15
+ N2RlMzUwMTZjOWI2ZTBiMjQwNzZhYjczODQ3ODRiZTdkMDRhNGU=
data/README.md CHANGED
@@ -38,7 +38,7 @@ To test the correct functioning of the `prerender_rails_embedded` gem just start
38
38
  with `rails s` and launch a curl to a (js generated) url like this:
39
39
 
40
40
  curl -H "User-Agent: Mozilla/5.0 baiduspider" \
41
- "http://localhost:4000/articles/a0af82a84-d386-4690-9f68-81ac768bc6d3"
41
+ "http://localhost:3000/articles/a0af82a84-d386-4690-9f68-81ac768bc6d3"
42
42
 
43
43
  It should show a completely rendered page in standard output.
44
44
 
@@ -85,6 +85,13 @@ and launch the web server with the command:
85
85
  bundle exec unicorn -p 3000 -c ./config/unicorn.rb
86
86
  ```
87
87
 
88
+ To test the actual invokation
89
+ ```shell
90
+ $ curl -H 'User-Agent: baiduspider' https://dev.etalia.net/articles/a6c37930c-a3df-482d-b9e5-b4333473e1ae
91
+ ```
92
+
93
+
94
+
88
95
  ### <a id='todos'></a>
89
96
  ## TODOs
90
97
 
@@ -8,46 +8,60 @@ var EVALUATE_JAVASCRIPT_CHECK_TIMEOUT = 50;
8
8
 
9
9
  var page = require('webpage').create(),
10
10
  system = require('system');
11
-
12
11
  var surrender = {};
12
+ page.viewportSize = {
13
+ width: 1024,
14
+ height: 800
15
+ };
13
16
 
14
17
  surrender.params = function() {
15
- if (system.args.length === 1) {
16
- throw 'Usage: googlizer.js <some URL>';
17
- }
18
- return { address: system.args[1]};
18
+ if (system.args.length === 1) {
19
+ throw 'Usage: googlizer.js <some URL>';
20
+ }
21
+ return { address: system.args[1]};
19
22
  };
20
23
 
24
+
21
25
  surrender.javascriptToExecuteOnPage = function() {
22
- try {
23
- var doctype = ''
24
- , html = document && document.getElementsByTagName('html');
25
-
26
- if(document.doctype) {
27
- doctype = "<!DOCTYPE "
28
- + document.doctype.name
29
- + (document.doctype.publicId ? ' PUBLIC "' + document.doctype.publicId + '"' : '')
30
- + (!document.doctype.publicId && document.doctype.systemId ? ' SYSTEM' : '')
31
- + (document.doctype.systemId ? ' "' + document.doctype.systemId + '"' : '')
32
- + '>';
33
- }
34
-
35
- if (html && html[0]) {
36
- return {
37
- html: doctype + html[0].outerHTML,
38
- shouldWaitForPrerenderReady: typeof window.prerenderReady === 'boolean',
39
- prerenderReady: window.prerenderReady
40
- };
41
- }
42
-
43
- } catch (e) {
44
- return {
45
- html: '',
46
- shouldWaitForPrerenderReady: false,
47
- prerenderReady: window.prerenderReady
48
- };
26
+ var processHtml = function() {
27
+ if (!window.prerenderRailsEmbedded || !window.prerenderRailsEmbedded.filterHtmlOutput || typeof(window.prerenderRailsEmbedded.filterHtmlOutput) !== "function") {
28
+ return document.documentElement.innerHTML;
29
+ } else {
30
+ return window.prerenderRailsEmbedded.filterHtmlOutput.call(this);
31
+ }
32
+ }
33
+
34
+ try {
35
+
36
+ if (!document) {
37
+ throw 'document is undefined. this function must be called inside page.evaluate ';
49
38
  }
39
+ var html = processHtml();
40
+ var doctype = '';
50
41
 
42
+ if(document && document.doctype) {
43
+ doctype = "<!DOCTYPE "
44
+ + document.doctype.name
45
+ + (document.doctype.publicId ? ' PUBLIC "' + document.doctype.publicId + '"' : '')
46
+ + (!document.doctype.publicId && document.doctype.systemId ? ' SYSTEM' : '')
47
+ + (document.doctype.systemId ? ' "' + document.doctype.systemId + '"' : '')
48
+ + '>';
49
+ }
50
+
51
+ return {
52
+ html: doctype + html,
53
+ shouldWaitForPrerenderReady: typeof window.prerenderReady === 'boolean',
54
+ prerenderReady: window.prerenderReady
55
+ };
56
+
57
+ } catch (e) {
58
+ console.log('ERROR: ', e);
59
+ return {
60
+ html: '',
61
+ shouldWaitForPrerenderReady: false,
62
+ prerenderReady: window.prerenderReady
63
+ };
64
+ }
51
65
  };
52
66
 
53
67
  surrender.serveHtml = function(html) {
@@ -56,31 +70,46 @@ surrender.serveHtml = function(html) {
56
70
  };
57
71
 
58
72
  surrender.evaluateJavascriptOnPage = function(t) {
59
- var _this = t || this
60
- , out = page.evaluate(_this.javascriptToExecuteOnPage);
73
+ var _this = t || this
74
+ , out = page.evaluate(_this.javascriptToExecuteOnPage);
61
75
 
62
- if(!out.shouldWaitForPrerenderReady || (out.shouldWaitForPrerenderReady && out.prerenderReady)) {
63
- _this.serveHtml(out.html);
64
- } else {
65
- setTimeout(function(){_this.evaluateJavascriptOnPage(_this)}, EVALUATE_JAVASCRIPT_CHECK_TIMEOUT);
66
- }
67
- return _this;
76
+ if(!out.shouldWaitForPrerenderReady || (out.shouldWaitForPrerenderReady && out.prerenderReady)) {
77
+ _this.serveHtml(out.html);
78
+ } else {
79
+ setTimeout(function(){_this.evaluateJavascriptOnPage(_this)}, EVALUATE_JAVASCRIPT_CHECK_TIMEOUT);
80
+ }
81
+ return _this;
68
82
  };
69
83
 
70
84
  surrender.run = function() {
71
- try {
72
- var _this = this;
73
- page.address = this.params().address;
74
-
75
- page.open(page.address, function (st) {
76
- if (st !== 'success') throw 'FAIL to load the address';
77
-
78
- _this.evaluateJavascriptOnPage();
79
- });
80
- } catch(e) {
81
- console.log(e);
82
- phantom.exit(1);
85
+ try {
86
+ var _this = this;
87
+ page.address = this.params().address;
88
+ page.settings.userAgent = 'EtaliaBotAgent';
89
+ page.open(page.address, function (st) {
90
+ if (st !== 'success') throw 'FAIL to load the address';
91
+ _this.evaluateJavascriptOnPage();
92
+ });
93
+ } catch(e) {
94
+ console.log(e);
95
+ phantom.exit(1);
96
+ }
97
+
98
+ page.onError = function(msg, trace) {
99
+ var msgStack = ['ERROR: ' + msg];
100
+ if (trace && trace.length) {
101
+ msgStack.push('TRACE:');
102
+ trace.forEach(function(t) {
103
+ msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
104
+ });
83
105
  }
106
+ console.log(msgStack.join('\n'));
107
+ };
108
+
109
+ page.onConsoleMessage = function(msg, lineNum, sourceId) {
110
+ console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
111
+ };
112
+
84
113
  };
85
114
 
86
115
  surrender.run();
@@ -2,8 +2,8 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'prerender_rails_embedded'
5
- spec.version = '0.1.1'
6
- spec.authors = ['Gian Carlo Pace']
5
+ spec.version = '0.2.0'
6
+ spec.authors = ['Gian Carlo Pace', 'Luca Mirra']
7
7
  spec.email = %w(giancarlo.pace@etalia.net)
8
8
  spec.description = %q{A plugin for prerender_rails middleware to render JavaScript web app on the fly calling phantomjs directly}
9
9
  spec.summary = %q{Prerender your backbone/angular/javascript rendered application on the fly when search engines crawl}
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prerender_rails_embedded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gian Carlo Pace
8
+ - Luca Mirra
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-04-16 00:00:00.000000000 Z
12
+ date: 2014-11-14 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -81,4 +82,3 @@ specification_version: 4
81
82
  summary: Prerender your backbone/angular/javascript rendered application on the fly
82
83
  when search engines crawl
83
84
  test_files: []
84
- has_rdoc: