diff options
author | Felipe Erias Morandeira <femorandeira@igalia.com> | 2013-07-02 14:21:39 +0100 |
---|---|---|
committer | Felipe Erias Morandeira <femorandeira@igalia.com> | 2013-07-02 14:24:15 +0100 |
commit | d401dc97e1c255078e47348652e560516709ae08 (patch) | |
tree | 96996f64dc163c456da7595d4bc1d63c7cfcb1a0 /test/smoke-tests | |
parent | 63bfea81df364fe3179229eeeba28ad16318ccc7 (diff) |
Separated the convenience library into its own file, which must be placed in /usr/share/gjs-1.0/ (or elsewhere in GJS's search path)
[endlessm/eos-sdk#113]
Diffstat (limited to 'test/smoke-tests')
-rw-r--r-- | test/smoke-tests/WebHelper.js | 86 | ||||
-rw-r--r-- | test/smoke-tests/webview.js | 93 |
2 files changed, 99 insertions, 80 deletions
diff --git a/test/smoke-tests/WebHelper.js b/test/smoke-tests/WebHelper.js new file mode 100644 index 0000000..8b78931 --- /dev/null +++ b/test/smoke-tests/WebHelper.js @@ -0,0 +1,86 @@ +const Endless = imports.gi.Endless; +const GLib = imports.gi.GLib; +const Gtk = imports.gi.Gtk; +const WebKit = imports.gi.WebKit; + +const EOS_URI_SCHEME = 'endless://'; + + +const Application = new Lang.Class({ + Name: 'WebApplication', + Extends: Endless.Application, + +// Set of actions that may be invoked from a WebView. +// Declare them as function(dict), and use links with the format +// "endless://actionName?parameter=value" + + _webActions: { }, + +// This callback does the translation from URI to action +// this._webview.connect('navigation-policy-decision-requested', +// Lang.bind(this, this._webHelper.onNavigationRequested)); + + _onNavigationRequested : function(web_view, frame, request, + navigation_action, policy_decision, + user_data) { + let uri = request.get_uri(); + + if(uri.indexOf(EOS_URI_SCHEME) == 0) { + // get the name and parameters for the desired function + let f_call = uri.substring(EOS_URI_SCHEME.length, uri.length).split('?'); + var function_name = f_call[0]; + var parameters = {}; + + if(f_call[1]) { + // there are parameters + let params = f_call[1].split('&'); + params.forEach(function(entry) { + let param = entry.split('='); + + if(param.length == 2) { + param[0] = decodeURIComponent(param[0]); + param[1] = decodeURIComponent(param[1]); + // and now we add it... + parameters[param[0]] = param[1]; + } + }); + } + + if(this._webActions[function_name]) + Lang.bind(this, this._webActions[function_name])(parameters); + else + print('Unknown function '+function_name); + + policy_decision.ignore(); + return true; + } else { + // this is a regular URL, just navigate there + return false; + } + }, + +// convenience functions + + _getElementById: function(webview, id) { + // WebKit.DOMDocument + let dom = webview.get_dom_document(); + + // WebKit.DOMElement + return dom.get_element_by_id(id); + }, + + _translateHTML: function(webview, lang) { + let dom = webview.get_dom_document(); + + // WebKit.DOMNodeList + let translatable = dom.get_elements_by_name('translatable'); + + for (var i = 0 ; i < translatable.get_length() ; i++) { + // WebKit.DOMNode + let element = translatable.item(i); + + // TODO here is where we would do the translation + element.inner_html = '<i>' + element.inner_text + '</i>'; + } + } +}); diff --git a/test/smoke-tests/webview.js b/test/smoke-tests/webview.js index 41d6836..9b84728 100644 --- a/test/smoke-tests/webview.js +++ b/test/smoke-tests/webview.js @@ -6,90 +6,18 @@ const GLib = imports.gi.GLib; const Gtk = imports.gi.Gtk; const WebKit = imports.gi.WebKit; -const TEST_APPLICATION_ID = 'com.endlessm.example.test-webview'; +// WebHelper.js must be copied somewhere in GJS's imports.searchPath +const WebHelper = imports.WebHelper; -const EOS_URI_SCHEME = 'endless://'; +const TEST_APPLICATION_ID = 'com.endlessm.example.test-webview'; const TestApplication = new Lang.Class({ Name: 'TestApplication', - Extends: Endless.Application, - - /* *** CONVENIENCE LIBRARY *** */ - - _onLoadStatus: function (web_view) { - if (web_view.load_status == WebKit.LoadStatus.FINISHED) { - // now we translate to Brazilian Portuguese - this._translateHTML (web_view, 'pt_BR'); - } - }, - - _onNavigationPolicyDecisionRequested: function(web_view, frame, request, - navigation_action, policy_decision, - user_data) { - // lame way of getting function name and parameters, without error check - - let uri = request.get_uri(); - - if(uri.indexOf(EOS_URI_SCHEME) == 0) { - // get the name and parameters for the desired function - print('URI: '+uri); - let f_call = uri.substring(EOS_URI_SCHEME.length, uri.length).split('?'); - var function_name = f_call[0]; - var parameters = {}; - - if(f_call[1]) { - // there are parameters - let params = f_call[1].split('&'); - params.forEach(function(entry) { - let param = entry.split('='); - - if(param.length == 2) { - param[0] = decodeURIComponent(param[0]); - param[1] = decodeURIComponent(param[1]); - // and now we add it... - parameters[param[0]] = param[1]; - } - }); - } - - if(this._html_actions[function_name]) - Lang.bind(this, this._html_actions[function_name])(parameters); - else - print('Unknown function '+function_name); - - policy_decision.ignore(); - return true; - } else { - return false; - } - }, - - _getElementById: function(webview, id) { - // WebKit.DOMDocument - let dom = webview.get_dom_document(); - - // WebKit.DOMElement - return dom.get_element_by_id(id); - }, - - _translateHTML: function(webview, lang) { - let dom = webview.get_dom_document(); - - // WebKit.DOMNodeList - let translatable = dom.get_elements_by_name('translatable'); - - for (var i = 0 ; i < translatable.get_length() ; i++) { - // WebKit.DOMNode - let element = translatable.item(i); - - // TODO here is where we would do the translation - element.inner_html = '<i>' + element.inner_text + '</i>'; - } - }, + Extends: WebHelper.Application, - /* *** ACTIONS AVAILABLE FROM HTML *** */ + /* *** ACTIONS AVAILABLE FROM THE WEB VIEW *** */ - _html_actions: { + _webActions: { /* dict['name'] is the name of the page to move to */ 'moveToPage': function(dict) { print('move to page '+dict['name']); @@ -142,10 +70,15 @@ const TestApplication = new Lang.Class({ this._webview.load_uri(GLib.filename_to_uri(target, null)); this._webview.connect('notify::load-status', - Lang.bind(this, this._onLoadStatus)); + Lang.bind(this, function (web_view, status) { + if (web_view.load_status == WebKit.LoadStatus.FINISHED) { + // now we translate to Brazilian Portuguese + this._translateHTML (web_view, 'pt_BR'); + } + })); this._webview.connect('navigation-policy-decision-requested', - Lang.bind(this, this._onNavigationPolicyDecisionRequested)); + Lang.bind(this, this._onNavigationRequested)); this._page1 = new Gtk.ScrolledWindow(); this._page1.add(this._webview); |