@taole/dev-helper 0.1.27-beta → 0.1.28

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.
@@ -117,6 +117,7 @@
117
117
  this._debug = false;
118
118
  this._defaultAppid = 'wx6cd4c28b58e8737f';// 点点开黑
119
119
  this._openAppTag = null;
120
+ this._openInstallButton = null;
120
121
  this._fallbackTime = 3000;
121
122
  this._appid = '';
122
123
  this._appInfo = null;
@@ -141,7 +142,6 @@
141
142
  this._openInstall = null;
142
143
  this._openInstallPromise = null;
143
144
  this._openInstallKey = '';
144
- this._openInstallPending = false;
145
145
  }
146
146
  _log(...args) {
147
147
  if (this._debug) {
@@ -164,6 +164,14 @@
164
164
  this._openAppTag = document.createElement('wx-open-launch-app');
165
165
  this._openAppTag.style.background = "transparent";
166
166
  this._openAppTag.style.opacity = 0;
167
+ this._openInstallButton = document.createElement('button');
168
+ this._openInstallButton.type = 'button';
169
+ this._openInstallButton.setAttribute('aria-label', '打开 App');
170
+ this._openInstallButton.style.width = '100%';
171
+ this._openInstallButton.style.height = '100%';
172
+ this._openInstallButton.addEventListener('click', () => {
173
+ if (this._useOpenInstall()) this._wakeupOpenInstall();
174
+ });
167
175
  this._updateTagAttr();
168
176
 
169
177
  var contentEle = document.createElement('div');
@@ -180,8 +188,9 @@
180
188
 
181
189
  this._openAppTag.appendChild(scriptTag);
182
190
  this._template.content.appendChild(this._openAppTag);
183
- this.attachShadow({ mode: 'open' });
184
- this.shadowRoot.appendChild(this._template.content);
191
+ this._template.content.appendChild(this._openInstallButton);
192
+ const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
193
+ shadow.appendChild(this._template.content);
185
194
  this._log('open-diandian: wx-open-launch-app appended');
186
195
  // 监听打开app失败的情况,跳转fallback链接
187
196
  this._openAppTag.addEventListener('error', (e) => {
@@ -190,10 +199,7 @@
190
199
  bubbles: true,
191
200
  composed: true,
192
201
  }));
193
- if (this._useOpenInstall()) {
194
- this._wakeupOpenInstall();
195
- return;
196
- }
202
+ if (this._useOpenInstall()) return;
197
203
  if (this._isHarmony || this._isSceneLink()) {
198
204
  this._showOpenInBrowserHint();
199
205
  return;
@@ -227,7 +233,7 @@
227
233
  return;
228
234
  }
229
235
  this._log('open-diandian: final fallback go', this._alink_fin_5s);
230
- this._openFallbackLink(this._alink_fin_5s);
236
+ this._openLink(this._alink_fin_5s);
231
237
  }, 5000);
232
238
  }
233
239
  });
@@ -250,8 +256,8 @@
250
256
  this._alink.style.width = '100%';
251
257
  this._alink.style.height = '100%';
252
258
  this._template.content.appendChild(this._alink);
253
- this.attachShadow({ mode: 'open' });
254
- this.shadowRoot.appendChild(this._template.content);
259
+ const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
260
+ shadow.appendChild(this._template.content);
255
261
  this._alink.addEventListener('click', this._handleAlinkClick.bind(this));
256
262
  this._log('open-diandian: open link appended');
257
263
  this._clickLink = false;
@@ -275,15 +281,14 @@
275
281
  }
276
282
  }
277
283
  _handleAlinkClick(event) {
284
+ const useOpenInstall = this._useOpenInstall();
285
+ if (useOpenInstall) {
286
+ event.preventDefault();
287
+ if (!this._openInstall) return;
288
+ }
278
289
  this._writeClickboard();
279
290
  this._cancelLaunchFallback();
280
- if (this._useOpenInstall()) {
281
- event.preventDefault();
282
- if (this._alink_link !== this._alink_fallback) {
283
- // 默认的 a 标签跳转发生在事件处理完成之后,先主动执行拉端。
284
- this._openFallbackLink(this._alink.href);
285
- }
286
- // 在同一次点击中调用,尽可能保留 SDK 写剪贴板所需的用户激活状态。
291
+ if (useOpenInstall) {
287
292
  this._wakeupOpenInstall();
288
293
  return;
289
294
  }
@@ -296,7 +301,7 @@
296
301
  this._cancelLaunchFallback();
297
302
  if (!this._isConnected || document.hidden) return;
298
303
  this._log('open app timed out, run fallback');
299
- this._openFallbackLink(this._alink_fallback);
304
+ this._openLink(this._alink_fallback);
300
305
  }, this._fallbackTime);
301
306
  }
302
307
  _cancelLaunchFallback() {
@@ -304,7 +309,7 @@
304
309
  this._alink_click_timer = null;
305
310
  this._clickLink = false;
306
311
  }
307
- _openFallbackLink(href) {
312
+ _openLink(href) {
308
313
  const link = document.createElement('a');
309
314
  link.href = href;
310
315
  link.click();
@@ -312,11 +317,12 @@
312
317
  disconnectedCallback() {
313
318
  this._log("disconnectedCallback");
314
319
  this._isConnected = false;
315
- this._openInstallPending = false;
316
320
  this._cancelLaunchFallback();
317
321
  if (this._openAppTag) {
318
322
  this._openAppTag.remove();
319
323
  this._openAppTag = null;
324
+ this._openInstallButton.remove();
325
+ this._openInstallButton = null;
320
326
  }
321
327
  if (this._alink) {
322
328
  this._alink.remove();
@@ -335,10 +341,10 @@
335
341
  }
336
342
  }
337
343
  _useOpenInstall() {
338
- return this.hasAttribute('openinstall') && !!(this._appInfo && this._appInfo.openinstall_app_key);
344
+ return this._isMobile && !this._isApp && this.hasAttribute('openinstall') && !!(this._appInfo && this._appInfo.openinstall_app_key);
339
345
  }
340
346
  _initOpenInstall() {
341
- if (!this._isConnected || this._isApp || !this._isMobile || !this._useOpenInstall()) return;
347
+ if (!this._isConnected || !this._useOpenInstall()) return;
342
348
  const key = this._appInfo.openinstall_app_key;
343
349
  if (this._openInstallKey === key && this._openInstallPromise) return;
344
350
  this._openInstallKey = key;
@@ -348,24 +354,31 @@
348
354
  promise.then(instance => {
349
355
  if (this._openInstallPromise !== promise) return;
350
356
  this._openInstall = instance;
351
- if (this._openInstallPending && this._isConnected && this._useOpenInstall()) {
352
- this._openInstallPending = false;
353
- instance.wakeupOrInstall();
354
- }
355
357
  }).catch(error => {
356
358
  if (this._openInstallPromise !== promise) return;
357
359
  this._openInstallPromise = null;
358
- this._openInstallPending = false;
359
360
  this._log('OpenInstall initialization failed', error);
360
361
  this.dispatchEvent(new CustomEvent('openinstallerror', { detail: { error }, bubbles: true, composed: true }));
361
362
  });
362
363
  }
363
364
  _wakeupOpenInstall() {
364
- if (this._openInstall) {
365
- this._openInstall.wakeupOrInstall();
365
+ if (!this._openInstall) return;
366
+ const extinfo = this.getAttribute('extinfo');
367
+ let data;
368
+ if (extinfo) {
369
+ try {
370
+ const info = JSON.parse(extinfo);
371
+ if (info && typeof info === 'object' && !Array.isArray(info)) {
372
+ data = { ...resolveOpenInstallData(), ...info };
373
+ }
374
+ } catch (error) {
375
+ this._log('invalid OpenInstall extinfo', error);
376
+ }
377
+ }
378
+ if (data) {
379
+ this._openInstall.wakeupOrInstall({ data });
366
380
  } else {
367
- this._openInstallPending = true;
368
- this._initOpenInstall();
381
+ this._openInstall.wakeupOrInstall();
369
382
  }
370
383
  }
371
384
  _initClipboardText() {
@@ -490,7 +503,6 @@
490
503
  this._openInstallKey = '';
491
504
  this._openInstall = null;
492
505
  this._openInstallPromise = null;
493
- this._openInstallPending = false;
494
506
  }
495
507
  var fallbackTime = this.getAttribute('fallback-time');
496
508
  if (fallbackTime == null || fallbackTime === '' || isNaN(Number(fallbackTime))) {
@@ -503,8 +515,9 @@
503
515
  console.error('open-diandian: appid not supported', this._appid);
504
516
  return;
505
517
  }
506
- if (this._isConnected) {
507
- if (!this._useOpenInstall()) this._openInstallPending = false;
518
+ if (this._isConnected && this._useOpenInstall()) {
519
+ this._cancelLaunchFallback();
520
+ this._cancelFinalFallback();
508
521
  this._initOpenInstall();
509
522
  }
510
523
  this._log('open-diandian: appInfo', this._appInfo);
@@ -524,13 +537,13 @@
524
537
  this._alink_fin_5s = this._appInfo.fallback_fin_5s || '';
525
538
  }
526
539
  if (this._openAppTag) {
540
+ const useOpenInstall = this._useOpenInstall();
541
+ this._openAppTag.style.display = useOpenInstall ? 'none' : '';
542
+ this._openInstallButton.hidden = !useOpenInstall;
527
543
  this._openAppTag.setAttribute('appid', this._appid);
528
544
  this._openAppTag.setAttribute('extinfo', this.getAttribute('extinfo') || "");
529
545
  } else if (this._alink) {
530
- // 开启 OpenInstall 时,由组件统一触发兜底,避免 Android Intent 直接跳转。
531
- this._alink.href = this._useOpenInstall() && this._alink_link.startsWith('intent:')
532
- ? this._alink_link.replace(/;S\.browser_fallback_url=[^;]*/, '')
533
- : this._alink_link;
546
+ this._alink.href = this._useOpenInstall() ? '#' : this._alink_link;
534
547
  }
535
548
  }
536
549
  };
@@ -112,6 +112,7 @@ function createOpenDiandianElementClass() {
112
112
  this._debug = false;
113
113
  this._defaultAppid = 'wx6cd4c28b58e8737f';// 点点开黑
114
114
  this._openAppTag = null;
115
+ this._openInstallButton = null;
115
116
  this._fallbackTime = 3000;
116
117
  this._appid = '';
117
118
  this._appInfo = null;
@@ -136,7 +137,6 @@ function createOpenDiandianElementClass() {
136
137
  this._openInstall = null;
137
138
  this._openInstallPromise = null;
138
139
  this._openInstallKey = '';
139
- this._openInstallPending = false;
140
140
  }
141
141
  _log(...args) {
142
142
  if (this._debug) {
@@ -159,6 +159,14 @@ function createOpenDiandianElementClass() {
159
159
  this._openAppTag = document.createElement('wx-open-launch-app');
160
160
  this._openAppTag.style.background = "transparent";
161
161
  this._openAppTag.style.opacity = 0;
162
+ this._openInstallButton = document.createElement('button');
163
+ this._openInstallButton.type = 'button';
164
+ this._openInstallButton.setAttribute('aria-label', '打开 App');
165
+ this._openInstallButton.style.width = '100%';
166
+ this._openInstallButton.style.height = '100%';
167
+ this._openInstallButton.addEventListener('click', () => {
168
+ if (this._useOpenInstall()) this._wakeupOpenInstall();
169
+ });
162
170
  this._updateTagAttr();
163
171
 
164
172
  var contentEle = document.createElement('div');
@@ -175,8 +183,9 @@ function createOpenDiandianElementClass() {
175
183
 
176
184
  this._openAppTag.appendChild(scriptTag);
177
185
  this._template.content.appendChild(this._openAppTag);
178
- this.attachShadow({ mode: 'open' });
179
- this.shadowRoot.appendChild(this._template.content);
186
+ this._template.content.appendChild(this._openInstallButton);
187
+ const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
188
+ shadow.appendChild(this._template.content);
180
189
  this._log('open-diandian: wx-open-launch-app appended');
181
190
  // 监听打开app失败的情况,跳转fallback链接
182
191
  this._openAppTag.addEventListener('error', (e) => {
@@ -185,10 +194,7 @@ function createOpenDiandianElementClass() {
185
194
  bubbles: true,
186
195
  composed: true,
187
196
  }));
188
- if (this._useOpenInstall()) {
189
- this._wakeupOpenInstall();
190
- return;
191
- }
197
+ if (this._useOpenInstall()) return;
192
198
  if (this._isHarmony || this._isSceneLink()) {
193
199
  this._showOpenInBrowserHint();
194
200
  return;
@@ -222,7 +228,7 @@ function createOpenDiandianElementClass() {
222
228
  return;
223
229
  }
224
230
  this._log('open-diandian: final fallback go', this._alink_fin_5s);
225
- this._openFallbackLink(this._alink_fin_5s);
231
+ this._openLink(this._alink_fin_5s);
226
232
  }, 5000);
227
233
  }
228
234
  });
@@ -245,8 +251,8 @@ function createOpenDiandianElementClass() {
245
251
  this._alink.style.width = '100%';
246
252
  this._alink.style.height = '100%';
247
253
  this._template.content.appendChild(this._alink);
248
- this.attachShadow({ mode: 'open' });
249
- this.shadowRoot.appendChild(this._template.content);
254
+ const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
255
+ shadow.appendChild(this._template.content);
250
256
  this._alink.addEventListener('click', this._handleAlinkClick.bind(this));
251
257
  this._log('open-diandian: open link appended');
252
258
  this._clickLink = false;
@@ -270,15 +276,14 @@ function createOpenDiandianElementClass() {
270
276
  }
271
277
  }
272
278
  _handleAlinkClick(event) {
279
+ const useOpenInstall = this._useOpenInstall();
280
+ if (useOpenInstall) {
281
+ event.preventDefault();
282
+ if (!this._openInstall) return;
283
+ }
273
284
  this._writeClickboard();
274
285
  this._cancelLaunchFallback();
275
- if (this._useOpenInstall()) {
276
- event.preventDefault();
277
- if (this._alink_link !== this._alink_fallback) {
278
- // 默认的 a 标签跳转发生在事件处理完成之后,先主动执行拉端。
279
- this._openFallbackLink(this._alink.href);
280
- }
281
- // 在同一次点击中调用,尽可能保留 SDK 写剪贴板所需的用户激活状态。
286
+ if (useOpenInstall) {
282
287
  this._wakeupOpenInstall();
283
288
  return;
284
289
  }
@@ -291,7 +296,7 @@ function createOpenDiandianElementClass() {
291
296
  this._cancelLaunchFallback();
292
297
  if (!this._isConnected || document.hidden) return;
293
298
  this._log('open app timed out, run fallback');
294
- this._openFallbackLink(this._alink_fallback);
299
+ this._openLink(this._alink_fallback);
295
300
  }, this._fallbackTime);
296
301
  }
297
302
  _cancelLaunchFallback() {
@@ -299,7 +304,7 @@ function createOpenDiandianElementClass() {
299
304
  this._alink_click_timer = null;
300
305
  this._clickLink = false;
301
306
  }
302
- _openFallbackLink(href) {
307
+ _openLink(href) {
303
308
  const link = document.createElement('a');
304
309
  link.href = href;
305
310
  link.click();
@@ -307,11 +312,12 @@ function createOpenDiandianElementClass() {
307
312
  disconnectedCallback() {
308
313
  this._log("disconnectedCallback")
309
314
  this._isConnected = false;
310
- this._openInstallPending = false;
311
315
  this._cancelLaunchFallback();
312
316
  if (this._openAppTag) {
313
317
  this._openAppTag.remove();
314
318
  this._openAppTag = null;
319
+ this._openInstallButton.remove();
320
+ this._openInstallButton = null;
315
321
  }
316
322
  if (this._alink) {
317
323
  this._alink.remove();
@@ -330,10 +336,10 @@ function createOpenDiandianElementClass() {
330
336
  }
331
337
  }
332
338
  _useOpenInstall() {
333
- return this.hasAttribute('openinstall') && !!(this._appInfo && this._appInfo.openinstall_app_key);
339
+ return this._isMobile && !this._isApp && this.hasAttribute('openinstall') && !!(this._appInfo && this._appInfo.openinstall_app_key);
334
340
  }
335
341
  _initOpenInstall() {
336
- if (!this._isConnected || this._isApp || !this._isMobile || !this._useOpenInstall()) return;
342
+ if (!this._isConnected || !this._useOpenInstall()) return;
337
343
  const key = this._appInfo.openinstall_app_key;
338
344
  if (this._openInstallKey === key && this._openInstallPromise) return;
339
345
  this._openInstallKey = key;
@@ -343,24 +349,31 @@ function createOpenDiandianElementClass() {
343
349
  promise.then(instance => {
344
350
  if (this._openInstallPromise !== promise) return;
345
351
  this._openInstall = instance;
346
- if (this._openInstallPending && this._isConnected && this._useOpenInstall()) {
347
- this._openInstallPending = false;
348
- instance.wakeupOrInstall();
349
- }
350
352
  }).catch(error => {
351
353
  if (this._openInstallPromise !== promise) return;
352
354
  this._openInstallPromise = null;
353
- this._openInstallPending = false;
354
355
  this._log('OpenInstall initialization failed', error);
355
356
  this.dispatchEvent(new CustomEvent('openinstallerror', { detail: { error }, bubbles: true, composed: true }));
356
357
  });
357
358
  }
358
359
  _wakeupOpenInstall() {
359
- if (this._openInstall) {
360
- this._openInstall.wakeupOrInstall();
360
+ if (!this._openInstall) return;
361
+ const extinfo = this.getAttribute('extinfo');
362
+ let data;
363
+ if (extinfo) {
364
+ try {
365
+ const info = JSON.parse(extinfo);
366
+ if (info && typeof info === 'object' && !Array.isArray(info)) {
367
+ data = { ...resolveOpenInstallData(), ...info };
368
+ }
369
+ } catch (error) {
370
+ this._log('invalid OpenInstall extinfo', error);
371
+ }
372
+ }
373
+ if (data) {
374
+ this._openInstall.wakeupOrInstall({ data });
361
375
  } else {
362
- this._openInstallPending = true;
363
- this._initOpenInstall();
376
+ this._openInstall.wakeupOrInstall();
364
377
  }
365
378
  }
366
379
  _initClipboardText() {
@@ -485,7 +498,6 @@ function createOpenDiandianElementClass() {
485
498
  this._openInstallKey = '';
486
499
  this._openInstall = null;
487
500
  this._openInstallPromise = null;
488
- this._openInstallPending = false;
489
501
  }
490
502
  var fallbackTime = this.getAttribute('fallback-time');
491
503
  if (fallbackTime == null || fallbackTime === '' || isNaN(Number(fallbackTime))) {
@@ -498,8 +510,9 @@ function createOpenDiandianElementClass() {
498
510
  console.error('open-diandian: appid not supported', this._appid);
499
511
  return;
500
512
  }
501
- if (this._isConnected) {
502
- if (!this._useOpenInstall()) this._openInstallPending = false;
513
+ if (this._isConnected && this._useOpenInstall()) {
514
+ this._cancelLaunchFallback();
515
+ this._cancelFinalFallback();
503
516
  this._initOpenInstall();
504
517
  }
505
518
  this._log('open-diandian: appInfo', this._appInfo);
@@ -519,13 +532,13 @@ function createOpenDiandianElementClass() {
519
532
  this._alink_fin_5s = this._appInfo.fallback_fin_5s || '';
520
533
  }
521
534
  if (this._openAppTag) {
535
+ const useOpenInstall = this._useOpenInstall();
536
+ this._openAppTag.style.display = useOpenInstall ? 'none' : '';
537
+ this._openInstallButton.hidden = !useOpenInstall;
522
538
  this._openAppTag.setAttribute('appid', this._appid);
523
539
  this._openAppTag.setAttribute('extinfo', this.getAttribute('extinfo') || "");
524
540
  } else if (this._alink) {
525
- // 开启 OpenInstall 时,由组件统一触发兜底,避免 Android Intent 直接跳转。
526
- this._alink.href = this._useOpenInstall() && this._alink_link.startsWith('intent:')
527
- ? this._alink_link.replace(/;S\.browser_fallback_url=[^;]*/, '')
528
- : this._alink_link;
541
+ this._alink.href = this._useOpenInstall() ? '#' : this._alink_link;
529
542
  }
530
543
  }
531
544
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/dev-helper",
3
- "version": "0.1.27-beta",
3
+ "version": "0.1.28",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "dev-helper": "./bin/dev-helper.js"