def __init__(self, runtime, persist_dir=None): self.storage = None if persist_dir is not None: try: try: os.makedirs(os.path.join(persist_dir, 'localstorage')) except OSError as e: if e.errno != errno.EEXIST: raise self.storage = dumbdbm.open( os.path.join(persist_dir, 'localstorage', str(runtime.pbw.uuid)), 'c') except IOError: pass if self.storage is None: logger.warning("Using transient store.") self.storage = _storage_cache.setdefault(str(runtime.pbw.uuid), {}) self.extension = v8.JSExtension( runtime.ext_name("localstorage"), """ (function() { native function _internal(); var proxy = _make_proxies({}, _internal(), ['set', 'has', 'delete_', 'keys', 'enumerate']); var methods = _make_proxies({}, _internal(), ['clear', 'getItem', 'setItem', 'removeItem', 'key']); proxy.get = function get(p, name) { return methods[name] || _internal().get(p, name); } this.localStorage = Proxy.create(proxy); })(); """, lambda f: lambda: self, dependencies=["runtime/internal/proxy"])
def __init__(self, runtime): self.extension = v8.JSExtension( runtime.ext_name("performance"), """ performance = new (function() { native function _time(); var start = _time(); this.now = function() { return (_time() - start) * 1000; }; })(); """, lambda f: lambda: time.time())
def __init__(self, runtime): self.runtime = runtime self.extension = v8.JSExtension( self.runtime.ext_name("console"), """ console = new (function () { native function _internal_console(); _make_proxies(this, _internal_console(), ['log', 'warn', 'info', 'error']); })(); """, lambda f: lambda: self, dependencies=["runtime/internal/proxy"])
def __init__(self, runtime): self.runtime = runtime self.timers = {} self.counter = 1 self.extension = v8.JSExtension( self.runtime.ext_name('timers'), """ (function() { native function _timers(); _make_proxies(this, _timers(), ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']); })(); """, lambda f: lambda: self, dependencies=["runtime/internal/proxy"])
def __init__(self, runtime): self._runtime = runtime self._runtime = runtime self.extension = v8.JSExtension(runtime.ext_name("navigator"), """ navigator = new (function() { native function _internal_location(); this.language = 'en-GB'; var location = _internal_location(); if(true) { // TODO: this should be a check on geolocation being enabled. this.geolocation = new (function() { _make_proxies(this, location, ['getCurrentPosition', 'watchPosition', 'clearWatch']); })(); } })(); """, lambda f: lambda: Geolocation(runtime), dependencies=["runtime/internal/proxy"])
def __init__(self, runtime, pebble): self.extension = v8.JSExtension(runtime.ext_name("pebble"), """ Pebble = new (function() { native function _internal_pebble(); _make_proxies(this, _internal_pebble(), ['sendAppMessage', 'showSimpleNotificationOnPebble', 'getAccountToken', 'getWatchToken', 'addEventListener', 'removeEventListener', 'openURL', 'getTimelineToken', 'timelineSubscribe', 'timelineUnsubscribe', 'timelineSubscriptions', 'getActiveWatchInfo', 'appGlanceReload']); this.platform = 'pypkjs'; })(); """, lambda f: lambda: self, dependencies=["runtime/internal/proxy"]) self.blobdb = pebble.blobdb self.pebble = pebble.pebble self.runtime = runtime self.tid = 0 self.uuid = runtime.pbw.uuid self.app_keys = runtime.pbw.manifest['appKeys'] self.pending_acks = {} self.is_ready = False self._timeline_token = None self._appmessage = self.runtime.runner.appmessage self._appmessage_handlers = [] super(Pebble, self).__init__(runtime)
from __future__ import absolute_import __author__ = 'katharine' import pypkjs.PyV8 as v8 import time import requests import pygeoip import os.path position = v8.JSExtension( "runtime/geolocation/position", """ Position = (function(coords, timestamp) { this.coords = coords; this.timestamp = timestamp; }); """) Position = lambda runtime, *args: v8.JSObject.create( runtime.context.locals.Position, args) coordinates = v8.JSExtension( "runtime/geolocation/coordinates", """ Coordinates = (function(long, lat, accuracy) { this.longitude = long this.latitude = lat this.accuracy = accuracy }); """) Coordinates = lambda runtime, *args: v8.JSObject.create( runtime.context.locals.Coordinates, args)
close_event = v8.JSExtension("runtime/events/ws", """ CloseEvent = function(eventInitDict) { Event.call(this, "close", eventInitDict); var wasClean = eventInitDict.wasClean; var code = eventInitDict.code; var reason = eventInitDict.reason; Object.defineProperties(this, { wasClean: { get: function() { return wasClean; }, enumerable: true, }, code: { get: function() { return code; }, enumerable: true, }, reason: { get: function() { return reason; }, enumerable: true, }, }); }; CloseEvent.prototype = Object.create(Event.prototype); CloseEvent.prototype.constructor = CloseEvent; MessageEvent = function(origin, data, eventInitDict) { Event.call(this, "message", eventInitDict); this.data = data; this.origin = origin; }; MessageEvent.prototype = Object.create(Event.prototype); MessageEvent.prototype.constructor = CloseEvent; """, dependencies=["runtime/event"])
from .exceptions import JSRuntimeException event = v8.JSExtension( "runtime/event", """ Event = function(event_type, event_init_dict) { var self = this; this.stopPropagation = function() {}; this.stopImmediatePropagation = function() { self._aborted = true; } this.preventDefault = function() { self.defaultPrevented = true; } this.initEvent = function(event_type, bubbles, cancelable) { self.type = event_type; self.bubbles = bubbles; self.cancelable = cancelable }; if(!event_init_dict) event_init_dict = {}; this.type = event_type; this.bubbles = event_init_dict.bubbles || false; this.cancelable = event_init_dict.cancelable || false; this.defaultPrevented = false; this.target = null; this.currentTarget = null; this.eventPhase = 2; this._aborted = false; }; Event.NONE = 0; Event.CAPTURING_PHASE = 1; Event.AT_TARGET = 2; Event.BUBBLING_PHASE = 3; """) Event = lambda runtime, *args: v8.JSObject.create(runtime.context.locals.Event,
from .safe_requests import NonlocalHTTPAdapter from .exceptions import JSRuntimeException progress_event = v8.JSExtension("runtime/events/progress", """ ProgressEvent = function(computable, loaded, total) { Event.call(this); computable = computable || false; loaded = loaded || 0; total = total || 0; Object.defineProperties(this, { lengthComputable: { get: function() { return computable; }, enumerable: true, }, loaded: { get: function() { return loaded; }, enumerable: true, }, total: { get: function() { return total; }, enumerable: true, }, }); } ProgressEvent.prototype = Object.create(Event.prototype); ProgressEvent.prototype.constructor = ProgressEvent; """, dependencies=["runtime/event"]) ProgressEvent = lambda runtime, *args: v8.JSObject.create( runtime.context.locals.ProgressEvent, args)
from .exceptions import JSRuntimeException logger = logging.getLogger('pypkjs.javascript.pebble') make_proxy_extension = v8.JSExtension( "runtime/internal/proxy", """ function _make_proxies(proxy, origin, names) { names.forEach(function(name) { proxy[name] = eval("(function " + name + "() { return origin[name].apply(origin, arguments); })"); }); return proxy; } function _make_properties(proxy, origin, names) { names.forEach(function(name) { Object.defineProperty(proxy, name, { configurable: false, enumerable: true, get: function() { return origin[name]; }, set: function(value) { origin[name] = value; } }); }); return proxy; } """) class JSRuntime(object): def __init__(self,