AppStore QR code

Add QRCode to iTunes AppStore page.

Fra 26.05.2025. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         AppStore QR code
// @namespace    https://www.iplaysoft.com
// @version      1.36
// @description  Add QRCode to iTunes AppStore page.
// @author       X-Force
// @match        https://apps.apple.com/*
// @grant        none
// @require      https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js
// @require      https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/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);
}



// URL简化
function simplifyAppUrl(url, removeCountry = false) {
    if (!url) return '';
    
    // 提取ID
    const idMatch = url.match(/\/id(\d+)/);
    if (!idMatch) return url;
    
    const appId = idMatch[1];
    
    if (removeCountry) {
    return `https://apps.apple.com/app/id${appId}`;
    } else {
    // 提取国家代码
    const countryMatch = url.match(/\/([a-z]{2})\/app\//i);
    const country = countryMatch ? countryMatch[1] : 'us';
    return `https://apps.apple.com/${country}/app/id${appId}`;
    }
}


function addQR(){
    var url = location.href.split('#')[0];
    var simpleUrl = simplifyAppUrl(url, false);
    //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 viewInItunes = "itmss://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=" + id + "&mt=8&at=10laHZ";
            var viewInAppStoreGrabber = "https://woo.im/appstore/app.php?url=" + encodeURIComponent(simpleUrl);
            console.log(viewInAppStoreGrabber);
            // var buttonDiv = document.createElement("div");
            // var buttonHtml = '<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>';
            // buttonHtml += '<a id="xf_itunes_link" target="_blank" href="' + viewInAppStoreGrabber + '">提取图片</a>';
            var buttonDiv = document.createElement("button");
            buttonDiv.className = "we-button we-button--outlined we-button--external icon icon-external we-button-fade-in";
            buttonDiv.innerHTML = '<a id="xf_itunes_link" target="_blank" href="' + viewInAppStoreGrabber + '">提取图片</a>';
            var targetObj = document.getElementsByClassName("product-header__routes__cta")[0] || document.getElementsByClassName("product-header__routes")[0] || document.getElementsByClassName("product-header")[0];

            if(targetObj) {
                targetObj.appendChild(buttonDiv);
            }else{
                console.log("找不到可插入的 targetObj");
            }


            var qrHtml = '<canvas id="qrcode" style="position:absolute;right:2px;top:64px;width:200px;height:200px"></canvas>';
            var qrDiv = document.createElement("div");
            qrDiv.innerHTML = qrHtml;
            var productHeader = document.getElementsByClassName("product-header")[0];
            if(productHeader) {
                productHeader.appendChild(qrDiv);
                document.getElementsByClassName("product-hero")[0].style.position = "relative";
            }
            new QRious({
                element: document.getElementById('qrcode'),
                value: simpleUrl, // Simplified URL
                size: 400
            });
        }
    }
}