From f3208fad41a7dbbb027d696b8c0a1db6d3bb0800 Mon Sep 17 00:00:00 2001 From: Lucian Branescu Mihaila Date: Tue, 28 Jul 2009 23:29:47 +0000 Subject: Workaround for saving data: URIs --- diff --git a/downloadmanager.py b/downloadmanager.py index f6a6eaf..d580d55 100644 --- a/downloadmanager.py +++ b/downloadmanager.py @@ -295,6 +295,11 @@ components.registrar.registerFactory('{23c51569-e9a1-4a92-adeb-3723db82ef7c}', def save_link(url, text, owner_document): # Inspired on Firefox' browser/base/content/nsContextMenu.js:saveLink() + # HACK, workaround for ticket #1029 + if url.startswith('data:'): + save_data_uri(url, text, owner_document) + return + cls = components.classes["@mozilla.org/network/io-service;1"] io_service = cls.getService(interfaces.nsIIOService) uri = io_service.newURI(url, None, None) @@ -309,7 +314,6 @@ def save_link(url, text, owner_document): interfaces.nsIRequest.LOAD_BYPASS_CACHE | \ interfaces.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS - # if _implements_interface(channel, interfaces.nsIHttpChannel): # trac#1029 if _implements_interface(channel, interfaces.nsIHttpChannel): channel.referrer = io_service.newURI(owner_document.documentURI, None, None) @@ -320,11 +324,54 @@ def save_link(url, text, owner_document): interfaces.nsIStreamListener) channel.asyncOpen(listener, None) +def save_data_uri(url, text, owner_document): + '''Special case, workaround for ticket #1029''' + import base64 + import re + + pattern = re.compile(r''' + ^data: + (\w+/\w+)? # mimetype + (?:;charset="(\w+)")? # charset + (?:;(base64))? # encoding + ,(.+) # actual data + $ + ''', re.VERBOSE) + result = re.search(pattern, url).groups() + logging.debug('^^^^^ %s' % str(result)) + + mime = result[0] or 'text/plain' + charset = result[1] or 'US-ASCII' + encoding = result[2] or 'base64' + data = base64.decodestring(result[3]) + + temp_path = os.path.join(activity.get_activity_root(), 'instance') + if not os.path.exists(temp_path): + os.makedirs(temp_path) + fd, file_path = tempfile.mkstemp(dir=temp_path, prefix='datauri', + suffix='') + os.close(fd) + os.chmod(file_path, 0644) + + # write data to file + open(file_path, 'w').write(data) + + jobject = datastore.create() + jobject.metadata['title'] = 'datauri' + jobject.metadata['mime_type'] = mime + jobject.metadata['icon-color'] = profile.get_color().to_string() + jobject.file_path = file_path + + datastore.write(jobject) + activity.show_object_in_journal(jobject.object_id) + + def _implements_interface(obj, interface): try: obj.QueryInterface(interface) return True except xpcom.Exception, e: + logging.debug('***** %s' % e.errno) if e.errno == NS_NOINTERFACE: return False else: diff --git a/usercode.py b/usercode.py index 7dbe945..e62b339 100644 --- a/usercode.py +++ b/usercode.py @@ -255,10 +255,12 @@ class Injector(): interfaces.nsIDOMEventListener) def handleEvent(self, event): + logging.debug('***** finish inject') + logging.debug('***** %s' % self.head.innerHTML) self.head.appendChild(self.script) - logging.debug('%^^%^^ actual inject') def attach_to(self, window): + logging.debug('***** starting inject') # set up the script element to be injected self.script = window.document.createElement('script') self.script.type = 'text/javascript' @@ -271,6 +273,8 @@ class Injector(): # actual attaching window.addEventListener('load', self._wrapped, False) + logging.debug('***** injecting ...') + class ScriptListener(gobject.GObject): _com_interfaces_ = interfaces.nsIWebProgressListener diff --git a/webactivity.py b/webactivity.py index 7f35f2a..fbad615 100644 --- a/webactivity.py +++ b/webactivity.py @@ -544,6 +544,7 @@ class WebActivity(activity.Activity): self._bm_store.add(name, url) def _userscript_found_cb(self, listener, location): + '''Ask user whether to install the userscript''' alert = ConfirmationAlert() alert.props.title = _('Add userscript') if usercode.script_exists(location): @@ -564,7 +565,7 @@ class WebActivity(activity.Activity): usercode.add_script(alert._location) def _userscript_inject_cb(self, listener, script_path): - logging.debug('Injecting %s' % script_path) + logging.debug('Injecting %s' % script_path) usercode.Injector(script_path).attach_to(self._browser.dom_window) def _add_link(self): -- cgit v0.9.1