appbyme.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //2.0定义
  2. function connectAppbymeJavascriptBridge(callback) {
  3. if (window.AppbymeJavascriptBridge) {
  4. callback(AppbymeJavascriptBridge)
  5. } else {
  6. document.addEventListener('connectAppbymeJavascriptBridge', function() {
  7. callback(AppbymeJavascriptBridge)
  8. }, false)
  9. }
  10. }
  11. //1.x
  12. var browser = {
  13. versions: function() {
  14. var a = navigator.userAgent,
  15. b = navigator.appVersion;
  16. return {
  17. trident: a.indexOf("Trident") > -1,
  18. presto: a.indexOf("Presto") > -1,
  19. webKit: a.indexOf("AppleWebKit") > -1,
  20. gecko: a.indexOf("Gecko") > -1 && a.indexOf("KHTML") == -1,
  21. mobile: !! a.match(/AppleWebKit.*Mobile.*/),
  22. ios: !! a.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
  23. android: a.indexOf("Android") > -1,
  24. iPhone: a.indexOf("iPhone") > -1,
  25. iPad: a.indexOf("iPad") > -1,
  26. webApp: a.indexOf("Safari") == -1,
  27. appbyme: a.indexOf("Appbyme") > -1
  28. }
  29. }(),
  30. language: (navigator.browserLanguage || navigator.language).toLowerCase()
  31. };
  32. function onLogout() {
  33. if (browser.versions.android) {
  34. appbyme.onLogout()
  35. } else {
  36. return document.location = "appbyme://onLogout"
  37. }
  38. }
  39. function onLogin() {
  40. if (browser.versions.android) {
  41. appbyme.onLogin()
  42. } else {
  43. return document.location = "appbyme://onLogin"
  44. }
  45. }
  46. function onShare(title, content, url) {
  47. if (browser.versions.android) {
  48. appbyme.onShare(title, content, url)
  49. } else {
  50. return document.location = "appbyme://onShare?"+encodeURIComponent(title)+"&"+encodeURIComponent(content)+"&"+encodeURIComponent(url);
  51. }
  52. }
  53. function getUserInfo() {
  54. if (browser.versions.android) {
  55. appbyme.getUserInfo()
  56. } else {
  57. return document.location = "appbyme://getUserInfo"
  58. }
  59. }
  60. function isAppbymeWeb() {
  61. if (browser.versions.appbyme) {
  62. return true
  63. }
  64. };
  65. var SHAKE_THRESHOLD = 3000;//默认阀值
  66. var last_update = 0;
  67. var x = y = z = last_x = last_y = last_z = 0;
  68. function initShake(threshold){
  69. if(threshold != null && threshold != ""){
  70. SHAKE_THRESHOLD = threshold;
  71. }
  72. if (browser.versions.android) {
  73. SHAKE_THRESHOLD = SHAKE_THRESHOLD + 1000;
  74. }
  75. if (window.DeviceMotionEvent) {
  76. window.addEventListener('devicemotion', deviceMotionHandler, false);
  77. } else {
  78. alert('您的设备不支持摇一摇功能');
  79. }
  80. }
  81. function deviceMotionHandler(eventData) {
  82. var acceleration = eventData.accelerationIncludingGravity;
  83. var curTime = new Date().getTime();
  84. if ((curTime - last_update) > 100) {
  85. var diffTime = curTime - last_update;
  86. last_update = curTime;
  87. x = acceleration.x;
  88. y = acceleration.y;
  89. z = acceleration.z;
  90. var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
  91. if (speed > SHAKE_THRESHOLD) {
  92. shakeCallBack();
  93. }
  94. last_x = x;
  95. last_y = y;
  96. last_z = z;
  97. }
  98. }