@taole/dev-helper 0.0.14 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.OpenDiandian = factory());
5
+ })(this, (function () { 'use strict';
6
+
7
+ const name = 'open-diandian';
8
+ // 测试使用open-diandain-app标签
9
+ function defineTag() {
10
+ customElements.define(
11
+ "open-diandain-app",
12
+ class extends HTMLElement {
13
+ static get observedAttributes() {
14
+ return ['appid', 'extinfo'];
15
+ }
16
+ constructor() {
17
+ super();
18
+ this._defaultAppid = 'wx6cd4c28b58e8737f';// 点点开黑
19
+ this._openAppTag = null;
20
+ this._template = null;
21
+ this._alink = null;
22
+ this._isMobile =
23
+ this._isApp = (window.AndroidJs && window.AndroidJs.publicInteraction) || (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.AndroidJs);
24
+ this._isWechat = /MicroMessenger/i.test(navigator.userAgent);
25
+ this._isAndroid = /Android/i.test(navigator.userAgent);
26
+ this._isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
27
+ this._isMobile = this._isAndroid || this._isIOS;
28
+ }
29
+ attributeChangedCallback() {
30
+ this._updateTagAttr();
31
+ }
32
+ connectedCallback() {
33
+ if (this._isWechat) {
34
+ this._genTemplate();
35
+ if (!this._openAppTag) {
36
+ // 放进去一个wx-open-launch-app, 宽高占满父容器
37
+ this._openAppTag = document.createElement('wx-open-launch-app');
38
+ this._updateTagAttr();
39
+
40
+ var contentEle = document.createElement('div');
41
+ contentEle.style.width = '1000px';
42
+ contentEle.style.height = '1000px';
43
+ contentEle.style.display = 'block';
44
+
45
+ var scriptFragment = document.createDocumentFragment();
46
+ scriptFragment.appendChild(contentEle);
47
+
48
+ var scriptTag = document.createElement('script');
49
+ scriptTag.setAttribute('type', 'text/wxtag-template');
50
+ scriptTag.appendChild(scriptFragment);
51
+
52
+ this._openAppTag.appendChild(scriptTag);
53
+ this._template.content.appendChild(this._openAppTag);
54
+ this.attachShadow({ mode: 'open' });
55
+ this.shadowRoot.appendChild(this._template.content);
56
+ console.log('open-diandian: wx-open-launch-app appended');
57
+ } else {
58
+ this._updateTagAttr();
59
+ }
60
+ } else if (!this._isApp && this._isMobile) {
61
+ this._genTemplate();
62
+ if (!this._alink) {
63
+ this._alink = document.createElement('a');
64
+ this._alink.style.display = 'block';
65
+ this._alink.style.width = '100%';
66
+ this._alink.style.height = '100%';
67
+ this._template.content.appendChild(this._alink);
68
+ this.attachShadow({ mode: 'open' });
69
+ this.shadowRoot.appendChild(this._template.content);
70
+ this._alink.addEventListener('click', this._handleAlinkClick.bind(this));
71
+ console.log('open-diandian: open link appended');
72
+ }
73
+ this._updateTagAttr();
74
+ }
75
+ }
76
+ _handleAlinkClick() {
77
+ // var nowTs = Date.now();
78
+ // console.log("this._alink is clicked", Date.now() - nowTs);
79
+ // 端外使用scheme打开app,3秒内如果页面依然可见,说明没有跳转到app里边,则跳转到fallback链接
80
+ if (this._alink_click_timer) {
81
+ clearTimeout(this._alink_click_timer);
82
+ }
83
+ let isPageHide = false;
84
+ this._pagehideCallback = this._pagehideCallback || function () {
85
+ // console.log("visibilitychange called", document.hidden, Date.now() - nowTs);
86
+ if (document.hidden) {
87
+ isPageHide = true;
88
+ }
89
+ };
90
+ window.removeEventListener("visibilitychange", this._pagehideCallback);
91
+ window.addEventListener("visibilitychange", this._pagehideCallback);
92
+ this._alink_click_timer = setTimeout(() => {
93
+ window.removeEventListener("visibilitychange", this._pagehideCallback);
94
+ setTimeout(() => {
95
+ // console.log("3s到了22", isPageHide, Date.now() - nowTs);
96
+ if (isPageHide) return;
97
+ this._alink_click_timer = null;
98
+ // window.location.href = this._alink_fallback; // 直接重定向的话ios上没法去到appstore
99
+ var tmplnk = document.createElement("a");
100
+ tmplnk.href = this._alink_fallback;
101
+ tmplnk.click();
102
+ }, 30);
103
+ }, 3000);
104
+ }
105
+ disconnectedCallback() {
106
+ if (this._openAppTag) {
107
+ this._openAppTag.remove();
108
+ this._openAppTag = null;
109
+ }
110
+ if (this._alink) {
111
+ this._alink.remove();
112
+ this._alink = null;
113
+ }
114
+ }
115
+ _genTemplate() {
116
+ if (!this._template) {
117
+ this._template = document.createElement('template');
118
+ this._template.innerHTML = '<style>:host {display: block;width: 100%;height: 100%;overflow: hidden;}</style>';
119
+ }
120
+ }
121
+ _updateTagAttr() {
122
+ if (this._openAppTag) {
123
+ this._openAppTag.setAttribute('appid', this.getAttribute('appid') || this._defaultAppid);
124
+ this._openAppTag.setAttribute('extinfo', this.getAttribute('extinfo') || "");
125
+ } else if (this._alink) {
126
+ // TODO: 如果设置了点点以外的appid,这里其实是有问题的,待后续更新优化
127
+ if (this._isIOS) {
128
+ this._alink.href = "com.tuwan.diandian://openPage?type=room&id=123";
129
+ this._alink_fallback = "https://apps.apple.com/cn/app/%E7%82%B9%E7%82%B9-%E8%AF%AD%E9%9F%B3%E5%BC%80%E9%BB%91/id1449564746";
130
+ } else {
131
+ var packageName = "com.tuwan.yw";
132
+ this._alink_fallback = "https://sj.qq.com/myapp/detail.htm?apkName=" + packageName;
133
+ this._alink.href = "intent://diandain:8888/splash#Intent;scheme=taole;package=" + packageName + ";S.browser_fallback_url=" + this._alink_fallback + ";end";
134
+ }
135
+ }
136
+ }
137
+ },
138
+ );
139
+ }
140
+
141
+ let isRegister = false;
142
+ function registerTdiv() {
143
+ if (!isRegister) {
144
+ defineTag();
145
+ isRegister = true;
146
+ }
147
+ }
148
+
149
+ registerTdiv();
150
+
151
+ // TODO: 之后放出setting入口之类的
152
+ var index = {
153
+ name
154
+ };
155
+
156
+ return index;
157
+
158
+ }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/dev-helper",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "npx -y rimraf dist && rollup -c",