From 7123670cff775b331e726e93118ccde066af89b5 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Thu, 8 Aug 2013 11:29:25 -0700 Subject: Custom image:// URI on WikipediaView WikipediaView will now interpret all image:// URIs as files in the Wikipedia images directory (whose root is set with the 'application-base-path' property on the WikipediaApplication.) [endlessm/eos-sdk#253] --- wikipedia/views/wikipedia_view.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'wikipedia') diff --git a/wikipedia/views/wikipedia_view.js b/wikipedia/views/wikipedia_view.js index 261e879..da4f942 100644 --- a/wikipedia/views/wikipedia_view.js +++ b/wikipedia/views/wikipedia_view.js @@ -1,3 +1,4 @@ +const Gio = imports.gi.Gio; const Gtk = imports.gi.Gtk; const Lang = imports.lang; const Soup = imports.gi.Soup; @@ -6,6 +7,16 @@ const Utils = imports.utils; const getPageURL = "http://127.0.0.1:3000/getArticleByTitle?title="; +// Interpret image:// URIs as wikipedia images +WebKit.WebContext.get_default().register_uri_scheme('image', function(request) { + let filename = request.get_uri().slice('image://'.length); + let pictures_dir = request.get_web_view()._getArticleImagesPath(); + let parent = Gio.File.new_for_path(pictures_dir); + let file = parent.get_child(filename); + let stream = file.read(null); + request.finish(stream, -1, 'image/png'); +}); + const WikipediaView = new Lang.Class({ Name: 'EndlessWikipediaView', Extends: WebKit.WebView, @@ -78,8 +89,7 @@ const WikipediaView = new Lang.Class({ let title = article['title']; let current_dir = Endless.getCurrentFileDir(); - let cur_exec = this.get_toplevel().get_application().application_base_path; - let image_path = cur_exec + "/web_view/article_images/"; + let image_path = this._getArticleImagesPath(); let documentHTML = this._get_meta_html() + this._get_body_html(articleHTML, human_title, image_path); @@ -94,5 +104,10 @@ const WikipediaView = new Lang.Class({ // Right now, this doesn't work, regardless of what we put in for base_uri this.load_uri(temp_uri); })); + }, + + _getArticleImagesPath: function() { + let cur_exec = this.get_toplevel().get_application().application_base_path; + return cur_exec + "/web_view/article_images/"; } }); -- cgit v1.2.3 From 90e9940a3d5e3b0cf98a6f264c2382b2e85ccdfc Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Thu, 8 Aug 2013 11:38:04 -0700 Subject: Do not build Wikipedia HTML in WikipediaView In order to avoid making Soup requests which trigger the crasher, literally load the response received from the node.js server. Remove all code that builds the HTML. [endlessm/eos-sdk#253] --- wikipedia/PrebuiltArticlesPage.js | 2 +- wikipedia/views/wikipedia_view.js | 73 ++------------------------------------- 2 files changed, 3 insertions(+), 72 deletions(-) (limited to 'wikipedia') diff --git a/wikipedia/PrebuiltArticlesPage.js b/wikipedia/PrebuiltArticlesPage.js index f5c133b..933e3b4 100644 --- a/wikipedia/PrebuiltArticlesPage.js +++ b/wikipedia/PrebuiltArticlesPage.js @@ -55,7 +55,7 @@ const PrebuiltArticlesPage = new Lang.Class({ set article_uri(value) { this._article_uri = value; if(value !== null && value !== "") { - this._wiki_view.loadArticleByTitle(this._article_uri, this._article_title); + this._wiki_view.loadArticleByTitle(this._article_uri); } }, diff --git a/wikipedia/views/wikipedia_view.js b/wikipedia/views/wikipedia_view.js index da4f942..f4a4500 100644 --- a/wikipedia/views/wikipedia_view.js +++ b/wikipedia/views/wikipedia_view.js @@ -22,7 +22,6 @@ const WikipediaView = new Lang.Class({ Extends: WebKit.WebView, _init: function(params) { - this._httpSession = new Soup.Session(); this.parent(params); // For debugging //let settings = this.get_settings(); @@ -31,79 +30,11 @@ const WikipediaView = new Lang.Class({ this.connect('context-menu', Lang.bind(this, function(){return true})); }, - _get_body_html:function(articleHTML, title, image_path){ - let html = ""; - html += "
"; - html += "
"; - html += "
"; - html += "

" + title + "

"; - html += "
"; - html += "
"; - html += "
"; - html += articleHTML; - html += "
"; - html += "
"; - html += "
"; - return html; - }, - - _get_style_sheet_html: function(current_dir, sheets){ - let html = ""; - for(let i = 0; i < sheets.length; i++){ - html += ""; - } - return html; - }, - - _get_script_html:function(current_dir, scripts){ - let html = ""; - for(let i = 0; i < scripts.length; i++){ - html += ""; - } - return html; - }, - - _get_meta_html:function(){ - let html = ""; - html += ""; - html += ""; - html += ""; - html += ""; - html += ""; - return html; - }, - - loadArticleByTitle: function(url, human_title) { + loadArticleByTitle: function(url) { let parts = url.split("/"); let suffix = parts[parts.length - 1]; let title = decodeURI(suffix.replace("_", " ", 'g')); - let request = Soup.Message.new("GET", getPageURL + title); - this._httpSession.queue_message(request, Lang.bind(this, function(_httpSession, message) { - if(message.status_code !== 200) { - print(message.status_code); - return; - } - let articleJSON = request.response_body.data; - let article = JSON.parse(articleJSON); - let articleHTML = article["text"]; - let title = article['title']; - let current_dir = Endless.getCurrentFileDir(); - - let image_path = this._getArticleImagesPath(); - - let documentHTML = this._get_meta_html() + this._get_body_html(articleHTML, human_title, image_path); - - let sheets = new Array("first_load.css", "second_load.css","main.css","wikipedia.css","nolinks.css"); - documentHTML = this._get_style_sheet_html(current_dir, sheets) + documentHTML; - - let scripts = new Array("jquery-min.js", "main.js"); - documentHTML = documentHTML + this._get_script_html(current_dir, scripts); - - let temp_uri = Utils.write_contents_to_temp_file("wiki.html", documentHTML); - // TODO: Ask about how we can load directly from HTML using load_html. - // Right now, this doesn't work, regardless of what we put in for base_uri - this.load_uri(temp_uri); - })); + this.load_uri(getPageURL + title); }, _getArticleImagesPath: function() { -- cgit v1.2.3 From b09ccf8994123ba4a62136bdc84d44579b4fe85d Mon Sep 17 00:00:00 2001 From: Rory MacQueen Date: Thu, 8 Aug 2013 12:35:59 -0700 Subject: Fixed URL encoding and correct API call [endlessm/eos-sdk#253] --- wikipedia/views/wikipedia_view.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'wikipedia') diff --git a/wikipedia/views/wikipedia_view.js b/wikipedia/views/wikipedia_view.js index f4a4500..bcd6270 100644 --- a/wikipedia/views/wikipedia_view.js +++ b/wikipedia/views/wikipedia_view.js @@ -5,11 +5,12 @@ const Soup = imports.gi.Soup; const WebKit = imports.gi.WebKit2; const Utils = imports.utils; -const getPageURL = "http://127.0.0.1:3000/getArticleByTitle?title="; +const getPageURL = "http://127.0.0.1:3000/getDomainSpecificArticle?title="; // Interpret image:// URIs as wikipedia images WebKit.WebContext.get_default().register_uri_scheme('image', function(request) { let filename = request.get_uri().slice('image://'.length); + filename = decodeURI(filename); let pictures_dir = request.get_web_view()._getArticleImagesPath(); let parent = Gio.File.new_for_path(pictures_dir); let file = parent.get_child(filename); -- cgit v1.2.3