Huawei AppGallery APK download

Add download APK links to application pages

// ==UserScript==
// @name        Huawei AppGallery APK download
// @namespace   appgallery_apk
// @description Add download APK links to application pages
// @icon        https://appgallery.huawei.com/static/agweb/img/ic_navigation_appmarket.png
// @include     https://appgallery.huawei.com/*
// @include     https://appgallery.cloud.huawei.com/*
// @version     1.0.0
// @grant       none
// ==/UserScript==

const isHash = '#/app/', isApp = '/app/';
var AppID = '';

if (document.location.pathname == '/' && document.location.hash.slice(0, isHash.length) == isHash) AppID = document.location.hash.slice(isHash.length)
  else if (document.location.pathname.slice(0, isApp.length) == isApp) AppID = document.location.pathname.slice(isApp.length);

if (AppID.length > 0)
{
  var TrimPos = AppID.search(/[\#\&\?\/]/);
  if (TrimPos < 0) TrimPos = Number.MAX_VALUE;
  AppID = AppID.slice(0, TrimPos);
  window.addEventListener('load', async function () {
    for (let i = 0; i < 100; i++)
    {
      var divtipcol = document.getElementsByClassName('tip');
      if (divtipcol.length != 0) break;
      await sleep(100);
    };
    if (divtipcol.length == 1)
    {
      var divtip = divtipcol[0];
      var tipitem = divtip.firstChild.cloneNode(true);
      tipitem.firstChild.remove();
      var apklink = document.createElement('a');
      apklink.href='https://appgallery.cloud.huawei.com/appdl/' + AppID;
      apklink.text = 'Download APK';
      tipitem.insertBefore(apklink, tipitem.firstChild);
      divtip.insertBefore(tipitem, divtip.firstChild);
    };
  });
};

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
};