// Copyright 2015 Endless Mobile, Inc. const Endless = imports.gi.Endless; const Gettext = imports.gettext; const Gtk = imports.gi.Gtk; const Lang = imports.lang; const WebHelper2 = imports.webhelper2; const WebKit2 = imports.gi.WebKit2; const TEST_APPLICATION_ID = 'com.endlessm.example.test-webview2'; const TEST_HTML = '\ \ \ First page \ \ \ \ \ \

First page

\ \

Move to page 2

\ \

Show \ message from parameter in this URL

\ \
\ \ \
\ \

Regular link to a Web site

\ \

This is text that will be italicized: Hello, \ world!

\ \

\ \ \

\ \ \ '; const TestApplication = new Lang.Class({ Name: 'TestApplication', Extends: Endless.Application, vfunc_dbus_register: function (connection, object_path) { this._webhelper = new WebHelper2.WebHelper({ well_known_name: this.application_id, connection: connection, }); return this.parent(connection, object_path); }, vfunc_startup: function () { this.parent(); this._webhelper.set_gettext((s) => s.italics()); this._webhelper.set_ngettext((s, p, n) => '** ' + (n == 1 ? s : p) + ' **'); this._webhelper.define_web_actions({ moveToPage: this.moveToPage.bind(this), showMessageFromParameter: this.showMessageFromParameter.bind(this), }); this._webview = new WebKit2.WebView(); this._webview.connect('load-changed', (webview, event) => { if (event === WebKit2.LoadEvent.FINISHED) this._webhelper.translate_html(webview, null, (obj, res) => { this._webhelper.translate_html_finish(res); }); }); this._webview.load_html(TEST_HTML, null); this._page2 = new Gtk.Grid(); let back_button = new Gtk.Button({ label: 'Go back to page 1' }); back_button.connect('clicked', () => { this._pm.visible_child_name = 'page1'; }); this._page2.add(back_button); this._window = new Endless.Window({ application: this, border_width: 16, }); this._pm = this._window.page_manager; this._pm.set_transition_type(Gtk.StackTransitionType.CROSSFADE); this._pm.add(this._webview, { name: 'page1' }); this._pm.add(this._page2, { name: 'page2' }); this._pm.visible_child_name = 'page1'; this._window.show_all(); }, vfunc_dbus_unregister: function (connection, object_path) { this.parent(connection, object_path); this._webhelper.unregister(); }, // WEB ACTIONS // dict['name'] is the name of the page to move to moveToPage: function (dict) { this._pm.visible_child_name = dict['name']; }, // dict['msg'] is the message to display showMessageFromParameter: function (dict) { let dialog = new Gtk.MessageDialog({ buttons: Gtk.ButtonsType.CLOSE, message_type: Gtk.MessageType.INFO, text: dict['msg'], transient_for: this._window, }); dialog.run(); dialog.destroy(); }, }); let app = new TestApplication({ application_id: TEST_APPLICATION_ID, }); app.run(ARGV);