page.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. (function (root, factory) {
  2. root.JsBridge = factory(root);
  3. }(typeof window == "undefined" ? this : window, function (win) {
  4. if (!win.document) { return {};}
  5. var doc = win.document,
  6. title = doc.title,
  7. ua = navigator.userAgent.toLowerCase(),
  8. platform = navigator.platform.toLowerCase(),
  9. isMacorWin = !(!platform.match("mac") && !platform.match("win")),
  10. isandroid = -1 != ua.indexOf("android"),
  11. isphoneorpad = -1 != ua.indexOf("iphone") || -1 != ua.indexOf("ipad"),
  12. JsBridge = {
  13. usable: false,
  14. init: function (bridge) {
  15. return this;
  16. },
  17. callJavaAsync: function(methodName, params, cb) {
  18. if (!window._JSNativeBridge) {
  19. //JS not be injected success
  20. cb({
  21. status: "-1",
  22. msg: "window._JSNativeBridge is undefined"
  23. }, {});
  24. return;
  25. }
  26. try {
  27. window._JSNativeBridge._doSendRequest(methodName, params, cb);
  28. } catch (e) {
  29. cb({status: "-1", msg: e},{});
  30. }
  31. },
  32. callJavaSync: function(methodName, params) {
  33. if (!window._JSNativeBridge) {
  34. return "error:JS not be injected success";
  35. }
  36. var result;
  37. try {
  38. result = window._JSNativeBridge._doSendRequestSync(methodName, params);
  39. } catch (e) {
  40. result = "error:" + e;
  41. }
  42. return result;
  43. },
  44. notification: function(params) {
  45. return this.callJavaSync("notification",{"msg": params});
  46. },
  47. picture: function(params,cb){
  48. this.callJavaAsync("picture",{},cb);
  49. },
  50. };
  51. if (window._JSNativeBridge) {
  52. JsBridge.init(window._JSNativeBridge);
  53. } else {
  54. document.addEventListener(
  55. 'JsBridgeInit',
  56. function(event) {
  57. JsBridge.init(event.bridge);
  58. }
  59. );
  60. }
  61. return JsBridge;
  62. }));