AppStore QR code

Add QRCode to iTunes AppStore page.

// ==UserScript==
// @name         AppStore QR code
// @namespace    https://www.iplaysoft.com
// @version      1.32
// @description  Add QRCode to iTunes AppStore page.
// @author       X-Force
// @match        https://apps.apple.com/*
// @grant        none
// @require      https://cdn.staticfile.org/jquery/3.6.3/jquery.min.js
// @require      https://cdn.staticfile.org/qrious/4.0.2/qrious.min.js
// @run-at document-start
// ==/UserScript==


//https://gist.github.com/BrockA/2625891#file-waitforkeyelements-js
//function waitForKeyElements(e,t,n,a){(o=void 0===a?$(e):$(a).contents().find(e))&&o.length>0?(l=!0,o.each((function(){var e=$(this);!e.data("alreadyFound")&&(t(e)?l=!1:e.data("alreadyFound",!0))}))):l=!1;var o,l,r=waitForKeyElements.controlObj||{},i=e.replace(/[^\w]/g,"_"),c=r[i];l&&n&&c?(clearInterval(c),delete r[i]):c||(c=setInterval((function(){waitForKeyElements(e,t,n,a)}),300),r[i]=c),waitForKeyElements.controlObj=r}
function waitForKeyElements(selectorTxt,actionFunction,bWaitOnce,iframeSelector){var targetNodes,btargetsFound;(targetNodes=void 0===iframeSelector?$(selectorTxt):$(iframeSelector).contents().find(selectorTxt))&&targetNodes.length>0?(btargetsFound=!0,targetNodes.each((function(){var jThis=$(this);jThis.data("alreadyFound")||!1||(actionFunction(jThis)?btargetsFound=!1:jThis.data("alreadyFound",!0))}))):btargetsFound=!1;var controlObj=waitForKeyElements.controlObj||{},controlKey=selectorTxt.replace(/[^\w]/g,"_"),timeControl=controlObj[controlKey];btargetsFound&&bWaitOnce&&timeControl?(clearInterval(timeControl),delete controlObj[controlKey]):timeControl||(timeControl=setInterval((function(){waitForKeyElements(selectorTxt,actionFunction,bWaitOnce,iframeSelector)}),300),controlObj[controlKey]=timeControl),waitForKeyElements.controlObj=controlObj}
console.log("Begin");
var $ = window.jQuery;
var runOnce=false;
var foundVideo=false;
var foundImage=false;

(function() {
    'use strict';
    //Story 下载标题图片
    var url = window.location.href;
    var matchStory = url.match(/https?:\/\/apps\.apple\.com\/.+?\/story\/id([0-9]+)/);

    if(matchStory!==null){
        //waitForKeyElements(".story-card--video>.story-card__content",addVideoDownloadLink)
        waitForKeyElements(".video-player--opaque",getVideoLink);

        //由于图片是 Lazy Load 的,所以需要用这个函数监视
        waitForKeyElements(".story-card .we-artwork__image--lazyload",getImgLink);
    }else{
        //添加 QR 代码
        // https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
        waitForKeyElements (".we-banner", addQR);
    }

    /*
    jquery 的函数在这里没用
    $(document).ready(function(){
        console.log("xxxcxcxcxcx");
    });
    */
})();

function getVideoLink(){
    foundVideo=true;
    if(runOnce){return;}
    runOnce=true;

    //    var myImgButton = document.createElement("div");
    //myImgButton.innerHTML = "xxxxxxxx";
    //insertAfter(myImgButton,document.getElementsByClassName("story-card")[0]);

    var stroyVideoCard=$(".story-card--video")[0];
    console.log(stroyVideoCard);
    if(stroyVideoCard!=undefined){
        var stroyVideoCardCSS=$(".story-card--video>.story-card__content")[0].style.cssText;
        stroyVideoCardCSS=decodeURI(stroyVideoCardCSS).replace(/\\/g, '');
        console.log("CSS: "+stroyVideoCardCSS);
        var re=/url\((.+?)\)/gm
        var matches = re.exec(stroyVideoCardCSS);
        if(matches && matches[1]){
             console.log(matches);
            var imgUrl= decodeURI(matches[1]);
            //addDownloadLinks(imgUrl);
        }

        var videoUrl=$(".story-card__video>.background-video")[0].shadowRoot.querySelector("div > video").src;
        console.log(videoUrl);
        addDownloadLinks(imgUrl,videoUrl);
    }
}


function getImgLink(){
    if(foundVideo || runOnce){
        return;
    }
    runOnce=true;
    console.log("Found Story ");

//    var imgSrcset=$(".story-card>.we-artwork>source[type='image/png']");
    var imgSrcset=$(".story-card>.we-artwork>source")[0];
    if(imgSrcset!=null){
        console.log(imgSrcset);
        var imgUrlStr=$(imgSrcset).attr('srcset').split(',').pop().trim().split(' ')[0];

        if(imgUrlStr!==null){
            var imgMatch=imgUrlStr.match(/(https?:\/\/.+?.(jpg|webp))\s*/);
            if(imgMatch!==null && imgMatch[1]!==null){
                var imgUrl = imgMatch[1];
                console.log(imgUrl);
                addDownloadLinks(imgUrl,null);
            }
        }
    }
}

function addDownloadLinks(imgUrl,videoUrl){
    var imgUrlDefault = imgUrl.replace(/\.(jpg|webp)/,".jpg");
    var imgUrlVertical = imgUrl.replace(/\/(\d+)x(\d+)(\w+?)\.(jpg|webp)/,"/0x4096h.jpg");
    var imgUrlHorizon = imgUrl.replace(/\/(\d+)x(\d+)(\w+?)\.(jpg|webp)/,"/4096x0w.jpg");

    var linkHtml='<a style="margin:0 auto;display:inline-block" target="_blank" href="'+imgUrlHorizon+'">下载封面图片 (横向)</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;';
    linkHtml=linkHtml+'<a style="margin:20px auto;display:inline-block" target="_blank" href="'+imgUrlVertical+'">下载竖向</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;';
    linkHtml=linkHtml+'<a style="margin:20px auto;display:inline-block" target="_blank" href="'+imgUrlDefault+'">封面图</a>';

    if(videoUrl!=null){
        linkHtml=linkHtml+'&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;';
        linkHtml=linkHtml+'<a style="margin:20px auto;display:inline-block" target="_blank" href="'+videoUrl+'">下载视频</a>';
    }

    var myImgButton = document.createElement("div");
    myImgButton.innerHTML = linkHtml;
    insertAfter(myImgButton,document.getElementsByClassName("story-card")[0]);
}


function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function addQR(){
    var url = location.href.split('#')[0];
    //iTunes 页面增加 QR Code
    if(document.title.match("Mac App Store")==null && url.match("\/app\/")){
        var regex = /\/id([0-9]+)/;
        var match = url.match(regex);
        var id = null;
        if(match!==null){
            id = match[1];
        }
        if(id!==null){
            var xurl = "itmss://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id="+id+"&mt=8&at=10laHZ";
            var mydiv = document.createElement("div");
            var html = '<style>#xf_itunes_link{display: inline-block;padding: 8px 22px;background: #228fff;color: #fff;font-size: 16px;border-radius: 6px;}#xf_itunes_link:hover{text-decoration:none}</style>';
            html = html + '<a id="xf_itunes_link" href="'+xurl+'">在 iTunes 中查看</a>';
            html = html + '<canvas id="qrcode" style="position:absolute;right:2px;top:64px;width:200px;height:200px"></canvas>';
            mydiv.innerHTML = html;
            document.getElementsByClassName("product-header")[0].appendChild(mydiv);
            document.getElementsByClassName("product-hero")[0].style.position="relative";
            var qrious = new QRious({
                element: document.getElementById('qrcode'),
                value: url,
                size: 400
            });
        }
    }
}