(function() {
    var code = [
        '(function() {',
        '  var _realAndroid = window.android;',
        '  window.android = new Proxy({}, {',
        '    get: function(_, method) {',
        '      if (method === "then") return;',
        '      return function() {',
        '        var args = Array.prototype.slice.call(arguments);',
        '        var result = prompt("bridge:" + method, JSON.stringify(args));',
        '        return result;',
        '      };',
        '    }',
        '  });',
        '  if (_realAndroid) {',
        '    Object.keys(_realAndroid).forEach(function(k) {',
        '      try { window.android[k] = _realAndroid[k]; } catch(e) {}',
        '    });',
        '  }',
        '  setInterval(function() {',
        '    var code = prompt("bridge:_poll", "");',
        '    if (code) { try { eval(code); } catch(e) {} }',
        '  }, 500);',
        '})();'
    ].join('\n');

    var script = document.createElement('script');
    script.textContent = code;
    var parent = document.head || document.documentElement;
    if (parent) {
        parent.appendChild(script);
        script.remove();
    }
})();
