source: content/bindings/vI_htmlTextBox.xml @ c5112d

ng_0.9
Last change on this file since c5112d was c5112d, checked in by rene <rene@…>, 4 years ago

newChannel is deprecated

  • Property mode set to 100644
File size: 10.2 KB
Line 
1<?xml version="1.0"?>
2<!-- ***** BEGIN LICENSE BLOCK *****
3   - This program is free software; you can redistribute it and/or modify
4   - it under the terms of the GNU General Public License as published by
5   - the Free Software Foundation; either version 2 of the License, or
6   - (at your option) any later version.
7
8   - This program is distributed in the hope that it will be useful,
9   - but WITHOUT ANY WARRANTY; without even the implied warranty of
10   - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   - GNU General Public License for more details.
12
13   - You should have received a copy of the GNU General Public License
14   - along with this program; if not, write to the Free Software
15   - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16
17   - The Original Code is the Virtual Identity Extension.
18
19   - The Initial Developer of the Original Code is Rene Ejury.
20   - code of remoteBrowserBox was copied and adapted from mozapps/update/updates.xml
21   - Portions created by the Initial Developer are Copyright (C) 2007
22   - the Initial Developer. All Rights Reserved.
23
24   - Contributor(s):
25   - ***** END LICENSE BLOCK ***** -->
26
27<!DOCTYPE overlay [
28<!ENTITY % vIdentDTD SYSTEM "chrome://v_identity/locale/vI_prefDialog.dtd">
29%vIdentDTD;
30]>
31
32<bindings id="vI_htmlTextBoxBindings"
33          xmlns="http://www.mozilla.org/xbl"
34          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
35          xmlns:xbl="http://www.mozilla.org/xbl">
36   
37    <binding id="virtualIdentityExtension_remoteBrowserBox">
38    <content>
39        <xul:vbox flex="1" >
40            <xul:vbox class="virtualIdentityExtension_remoteBrowserBoxProgressmeter" flex="1" >
41                <xul:progressmeter mode="undetermined" />
42                <xul:spacer flex="1" />
43            </xul:vbox>
44            <xul:browser xbl:inherits="src=url" type="content" class="vI_htmlTextBox" hidden="true" flex="1" disablehistory="true" />
45            <xul:button xbl:inherits="tooltiptext=url" label="&vI_prefDialog.openHelpInBrowserButton.label;"
46                    oncommand="virtualIdentityExtension.prefDialog.openURL(this.tooltipText); virtualIdentityExtension.prefDialog.toggleHelp();" />
47        </xul:vbox>
48    </content>
49    <resources>
50            <stylesheet src="chrome://v_identity/skin/v_identity.css"/>
51    </resources>
52    <implementation>
53    <destructor><![CDATA[
54        // clean up the listener but you may not have one if you never showed
55        // the page with a <remotecontent> element
56        if (this._remoteProgressListener) this._content.webProgress.removeProgressListener(this._remoteProgressListener);
57    ]]></destructor>
58    <field name="_init">null</field>
59    <method name="init">
60    <body><![CDATA[
61        if (this._init) return;
62       
63        // just to be safe, turn off js, auth, plugins, and subframes
64        var docShell = this._content.docShell;
65        docShell.allowJavascript = false;
66        docShell.allowAuth = false;
67        docShell.allowPlugins = false;
68        //allow redirects
69        //docShell.allowMetaRedirects = false;
70        docShell.allowSubframes = false;
71        //allow images
72        //docShell.allowImages = false;
73       
74        this._init = true;
75    ]]></body>
76    </method>
77    <field name="_remoteProgressListener">null</field>
78    <property name="url">
79        <getter><![CDATA[
80        return this.getAttribute("url");
81        ]]></getter>
82        <setter><![CDATA[
83        this.init();
84        if (this._remoteProgressListener)
85            this._content.webProgress.removeProgressListener(this._remoteProgressListener);
86        var self = this;
87
88        this._remoteProgressListener = {
89            QueryInterface: function(aIID) {
90                if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
91                    aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
92                    aIID.equals(Components.interfaces.nsISupports))
93                    return this;
94                throw Components.results.NS_NOINTERFACE;
95            },
96            onLocationChange: function() {},
97            onProgressChange: function() {},
98            onStatusChange:   function() {},
99            onSecurityChange: function() {},
100            onStateChange:    function(aWebProgress, aRequest, aStateFlags, aStatus) {
101                if (!aRequest)
102                    return;
103
104                const nsIWebProgressListener =
105                    Components.interfaces.nsIWebProgressListener;
106           
107                if (aStateFlags & nsIWebProgressListener.STATE_IS_NETWORK)
108                {
109                    if (aStateFlags & nsIWebProgressListener.STATE_START) self.onStart();
110                    else if (aStateFlags & nsIWebProgressListener.STATE_STOP) {
111                        const nsIHttpChannel = Components.interfaces.nsIHttpChannel;
112                        try {
113                            var channel = aRequest.QueryInterface(nsIHttpChannel);
114                            if (channel.requestSucceeded) {
115                                // if we get here, the load was OK, report success
116                                self.onLoad();
117                                return;
118                            }
119                        } catch (ex) {
120                            // the channel.requestSucceeded can throw
121                            // NS_ERROR_NOT_AVAILABLE if you can't reach the server
122                            // so we treat that as an error
123                        }
124                        // everything else (like a 404) is an error
125                        self.onError();
126                    }
127                }
128            }
129        }
130        this._content.webProgress.addProgressListener(this._remoteProgressListener,Components.interfaces.nsIWebProgress.NOTIFY_ALL);
131        // load the url now that the listener is registered
132        this.setAttribute("url", val);
133        ]]></setter>
134    </property>
135    <field name="_content">
136        document.getAnonymousElementByAttribute(this, "class", "vI_htmlTextBox");
137    </field>
138    <field name="_progress">
139        document.getAnonymousElementByAttribute(this, "class", "virtualIdentityExtension_remoteBrowserBoxProgressmeter");
140    </field>
141    <property name="localCss" onget="return this.getAttribute('localCss');" onset="this.setAttribute('localCss', val); return val;" />
142    <method name="onError">
143    <body><![CDATA[
144    //  alert("error");
145    ]]></body>
146    </method>
147    <method name="onLoad">
148        <body><![CDATA[
149        this._content.cssSource = this.localCss;
150        this._progress.setAttribute("hidden", "true");
151        this._content.removeAttribute("hidden");
152    ]]></body>
153    </method>
154    <method name="onStart">
155    <body><![CDATA[
156        this._content.setAttribute("hidden", "true");
157        this._progress.removeAttribute("hidden");
158    ]]></body>
159    </method>
160    </implementation>
161    </binding>
162
163
164    <binding id="vI_htmlTextBox" extends="chrome://global/content/bindings/browser.xml#browser">
165    <implementation>
166    <constructor>
167        <![CDATA[       
168        virtualIdentityExtension.MyLog.debug('******* vI_htmlTextBox.xml constructor');
169        ]]>
170    </constructor>
171    <field name="_cssRead">false</field>
172    <property name="cssSource" >
173        <setter><![CDATA[
174        if (!val) return;
175        var head = this.contentDocument.getElementsByTagName("HEAD").item(0);
176        var range = document.createRange();
177        range.selectNode(head);
178        var css_text = this._getContents("chrome://v_identity/skin/" + val);
179        var documentFragment = range.createContextualFragment("<style type='text/css'>" + css_text + "</style>");
180        head.appendChild(documentFragment);
181        virtualIdentityExtension.MyLog.debug("cssSource set to " + val + " done");
182        this._cssRead = true;
183        ]]></setter>
184    </property>
185    <method name="_getContents">
186        <parameter name="aURL" />
187        <body><![CDATA[     
188        // read the chrome file (copied from http://forums.mozillazine.org/viewtopic.php?p=921150)
189        var ioService=Components.classes["@mozilla.org/network/io-service;1"]
190            .getService(Components.interfaces.nsIIOService);
191        var scriptableStream=Components
192            .classes["@mozilla.org/scriptableinputstream;1"]
193            .getService(Components.interfaces.nsIScriptableInputStream);
194       
195        var channel=ioService.newChannel2(aURL,null,null);
196        var input=channel.open();
197        scriptableStream.init(input);
198        var str=scriptableStream.read(input.available());
199        scriptableStream.close();
200        input.close();
201        return str;
202        ]]></body>
203    </method>
204    <property name="outputString" >
205        <setter><![CDATA[
206        if (!this._cssRead) this.cssSource = this.getAttribute('cssSource');    // read initial Attributes
207        this.contentDocument.lastChild.setAttribute("style", this.getAttribute('documentStyle'));
208       
209        var oldChilds = this.contentDocument.body.childNodes;
210        while (oldChilds.length > 0) this.contentDocument.body.removeChild(oldChilds[0])
211
212        var text_list = val.split(/\n/)
213        for (var i = 0; i < text_list.length; i++) {
214            if (this._isHR(text_list[i])) continue;
215            var text_item = this._checkList(text_list[i]);
216            this._add(text_item);
217            if (!this._currentList && (i + 1) < text_list.length) {
218                var new_br = this.contentDocument
219                    .createElementNS("http://www.w3.org/1999/xhtml", 'html:br');
220                this.contentDocument.body.appendChild(new_br);
221            }
222        }
223        ]]></setter>
224    </property>
225    <field name="_currentList">null</field>
226    <field name="_currentBaseNode">null</field>
227    <method name="_isHR">
228        <parameter name="text" />
229        <body><![CDATA[     
230        if (text == "---") {
231            var new_hr = this.contentDocument
232                .createElementNS("http://www.w3.org/1999/xhtml", 'html:hr');
233            this.contentDocument.body.appendChild(new_hr);
234            return true;
235        }
236        return false;
237        ]]></body>
238    </method>
239    <method name="_checkList">
240        <parameter name="text" />
241        <body><![CDATA[
242        if (text.match(/^\*\s/)) {
243            if (!this._currentList) {
244                this._currentList = this.contentDocument
245                    .createElementNS("http://www.w3.org/1999/xhtml", 'html:ul');
246                this.contentDocument.body.appendChild(this._currentList);
247            }
248            this._currentBaseNode = this.contentDocument
249                                .createElementNS("http://www.w3.org/1999/xhtml", 'html:li');
250            this._currentList.appendChild(this._currentBaseNode);
251        }
252        else {
253            this._currentList = null;
254            this._currentBaseNode = this.contentDocument.body;
255        }
256        return text.replace(/^\*\s/,"")
257        ]]></body>
258    </method>
259    <method name="_add">
260        <parameter name="text" />
261        <body><![CDATA[
262        if (text.match(/http[s]?:\/\/\S+/)) {
263            if (RegExp.leftContext) this._addText(RegExp.leftContext)
264            if (RegExp.lastMatch) this._addLink(RegExp.lastMatch)
265            if (RegExp.rightContext) this._add(RegExp.rightContext)
266        }
267        else this._addText(text);
268        ]]></body>
269    </method>
270    <method name="_addText">
271        <parameter name="text" />
272        <body><![CDATA[     
273        var range = document.createRange();
274        range.selectNode(this._currentBaseNode);
275        var documentFragment = range.createContextualFragment(text);
276        this._currentBaseNode.appendChild(documentFragment);
277        ]]></body>
278    </method>
279    <method name="_addLink">
280        <parameter name="text" />
281        <body><![CDATA[     
282        var new_a = this.contentDocument
283            .createElementNS("http://www.w3.org/1999/xhtml", 'html:a');
284        new_a.setAttribute("href", text)
285        new_a.setAttribute("style", "text-decoration: underline")
286        this._currentBaseNode.appendChild(new_a);
287        var new_text = this.contentDocument.createTextNode(text);
288        new_a.appendChild(new_text);
289        ]]></body>
290    </method>
291    </implementation>
292    </binding>
293
294</bindings>
Note: See TracBrowser for help on using the repository browser.