Skip to content

Commit

Permalink
this fixes gorhill#1579
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Apr 22, 2016
1 parent d34d470 commit 8c02460
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 145 deletions.
196 changes: 124 additions & 72 deletions platform/chromium/vapi-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,118 +384,170 @@ vAPI.shutdown.add(function() {
"(function() {",
" 'use strict';",
"",
" var WS = window.WebSocket;",
" var Wrapped = window.WebSocket;",
" var toWrapped = new WeakMap();",
"",
" var onClose = function(ev) {",
" var wrapped = toWrapped.get(this);",
" if ( !wrapped ) {",
" var onResponseReceived = function(wrapper, ok) {",
" this.onload = this.onerror = null;",
" var bag = toWrapped.get(wrapper);",
" if ( !ok ) {",
" if ( bag.properties.onerror ) {",
" bag.properties.onerror(new window.ErrorEvent('error'));",
" }",
" return;",
" }",
" this.readyState = wrapped.readyState;",
" if ( this.onclose !== null ) {",
" this.onclose(ev);",
" var wrapped = new Wrapped(bag.args.url, bag.args.protocols);",
" for ( var prop in bag.properties ) {",
" wrapped[prop] = bag.properties[prop];",
" }",
" toWrapped.set(wrapper, wrapped);",
" };",
"",
" var onError = function(ev) {",
" var wrapped = toWrapped.get(this);",
" var noopfn = function() {",
" };",
"",
" var fallthruGet = function(wrapper, prop, value) {",
" var wrapped = toWrapped.get(wrapper);",
" if ( !wrapped ) {",
" return;",
" return value;",
" }",
" this.readyState = wrapped.readyState;",
" if ( this.onerror !== null ) {",
" this.onerror(ev);",
" if ( wrapped instanceof Wrapped ) {",
" return wrapped[prop];",
" }",
" return wrapped.properties.hasOwnProperty(prop) ?",
" wrapped.properties[prop] :",
" value;",
" };",
"",
" var onMessage = function(ev) {",
" if ( this.onmessage !== null ) {",
" this.onmessage(ev);",
" var fallthruSet = function(wrapper, prop, value) {",
" if ( value instanceof Function ) {",
" value = value.bind(wrapper);",
" }",
" };",
"",
" var onOpen = function(ev) {",
" var wrapped = toWrapped.get(this);",
" var wrapped = toWrapped.get(wrapper);",
" if ( !wrapped ) {",
" return;",
" }",
" this.readyState = wrapped.readyState;",
" if ( this.onopen !== null ) {",
" this.onopen(ev);",
" if ( wrapped instanceof Wrapped ) {",
" wrapped[prop] = value;",
" } else {",
" wrapped.properties[prop] = value;",
" }",
" };",
"",
" var onAllowed = function(ws, url, protocols) {",
" this.removeEventListener('load', onAllowed);",
" this.removeEventListener('error', onBlocked);",
" connect(ws, url, protocols);",
" };",
"",
" var onBlocked = function(ws) {",
" this.removeEventListener('load', onAllowed);",
" this.removeEventListener('error', onBlocked);",
" if ( ws.onerror !== null ) {",
" ws.onerror(new window.ErrorEvent('error'));",
" var WebSocket = function(url, protocols) {",
" if ( window.location.protocol === 'https:' && /^ws:/.test(url) ) {",
" var ws = new Wrapped(url, protocols);",
" if ( ws ) {",
" ws.close();",
" }",
" }",
" };",
"",
" var connect = function(wrapper, url, protocols) {",
" var wrapped = new WS(url, protocols);",
" toWrapped.set(wrapper, wrapped);",
" wrapped.onclose = onClose.bind(wrapper);",
" wrapped.onerror = onError.bind(wrapper);",
" wrapped.onmessage = onMessage.bind(wrapper);",
" wrapped.onopen = onOpen.bind(wrapper);",
" };",
"",
" var WebSocket = function(url, protocols) {",
" this.binaryType = '';",
" this.bufferedAmount = 0;",
" this.extensions = '';",
" this.onclose = null;",
" this.onerror = null;",
" this.onmessage = null;",
" this.onopen = null;",
" this.protocol = '';",
" this.readyState = this.CONNECTING;",
" this.url = url;",
" Object.defineProperties(this, {",
" 'binaryType': {",
" get: function() {",
" return fallthruGet(this, 'binaryType', '');",
" },",
" set: function(value) {",
" fallthruSet(this, 'binaryType', value);",
" }",
" },",
" 'bufferedAmount': {",
" get: function() {",
" return fallthruGet(this, 'bufferedAmount', 0);",
" },",
" set: noopfn",
" },",
" 'extensions': {",
" get: function() {",
" return fallthruGet(this, 'extensions', '');",
" },",
" set: noopfn",
" },",
" 'onclose': {",
" get: function() {",
" return fallthruGet(this, 'onclose', null);",
" },",
" set: function(value) {",
" fallthruSet(this, 'onclose', value);",
" }",
" },",
" 'onerror': {",
" get: function() {",
" return fallthruGet(this, 'onerror', null);",
" },",
" set: function(value) {",
" fallthruSet(this, 'onerror', value);",
" }",
" },",
" 'onmessage': {",
" get: function() {",
" return fallthruGet(this, 'onmessage', null);",
" },",
" set: function(value) {",
" fallthruSet(this, 'onmessage', value);",
" }",
" },",
" 'onopen': {",
" get: function() {",
" return fallthruGet(this, 'onopen', null);",
" },",
" set: function(value) {",
" fallthruSet(this, 'onopen', value);",
" }",
" },",
" 'protocol': {",
" get: function() {",
" return fallthruGet(this, 'protocol', '');",
" },",
" set: noopfn",
" },",
" 'readyState': {",
" get: function() {",
" return fallthruGet(this, 'readyState', 0);",
" },",
" set: noopfn",
" },",
" 'url': {",
" get: function() {",
" return fallthruGet(this, 'url', '');",
" },",
" set: noopfn",
" }",
" });",
"",
" if ( /^wss?:\\/\\//.test(url) === false ) {",
" connect(this, url, protocols);",
" return;",
" }",
" toWrapped.set(this, {",
" args: { url: url, protocols: protocols },",
" properties: {}",
" });",
"",
" var img = new Image();",
" img.src = ",
" window.location.origin",
" + '?url=' + encodeURIComponent(url)",
" + '&ubofix=f41665f3028c7fd10eecf573336216d3';",
" img.addEventListener('load', onAllowed.bind(img, this, url, protocols));",
" img.addEventListener('error', onBlocked.bind(img, this, url, protocols));",
" img.onload = onResponseReceived.bind(img, this, true);",
" img.onerror = onResponseReceived.bind(img, this, false);",
" };",
"",
" WebSocket.prototype.CONNECTING = 0;",
" WebSocket.prototype.OPEN = 1;",
" WebSocket.prototype.CLOSING = 2;",
" WebSocket.prototype.CLOSED = 3;",
"",
" WebSocket.prototype.close = function(code, reason) {",
" var wrapped = toWrapped.get(this);",
" if ( !wrapped ) {",
" return;",
" if ( wrapped instanceof Wrapped ) {",
" wrapped.close(code, reason);",
" }",
" wrapped.close(code, reason);",
" };",
"",
" WebSocket.prototype.send = function(data) {",
" var wrapped = toWrapped.get(this);",
" if ( !wrapped ) {",
" return;",
" if ( wrapped instanceof Wrapped ) {",
" wrapped.send(data);",
" }",
" wrapped.send(data);",
" };",
"",
" WebSocket.prototype.CONNECTING = 0;",
" WebSocket.prototype.OPEN = 1;",
" WebSocket.prototype.CLOSING = 2;",
" WebSocket.prototype.CLOSED = 3;",
"",
" window.WebSocket = WebSocket;",
"",
" var me = document.getElementById('ubofix-f41665f3028c7fd10eecf573336216d3');",
Expand Down
Loading

0 comments on commit 8c02460

Please sign in to comment.