Delete Aliyun Old Docker Image

try to take over the world!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Delete Aliyun Old Docker Image
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://cr.console.aliyun.com/repository/*/*/*/images
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    var retryCheck = function(checkFun,interval,nextFun,times,delay,startTime){
        if(!times)times = 1;
        else times += 1;
        if(!delay)delay = 0;
        if(!startTime)startTime = (new Date()).getTime();
        setTimeout(function(){
            if(checkFun(times)){
                if(delay){
                    var detal = delay - ((new Date()).getTime() - startTime);
                    if(detal>0)setTimeout(nextFun,detal);
                    else nextFun();
                }else nextFun();
            }else retryCheck(checkFun,interval,nextFun,times,delay,startTime);
        },interval);
    }
    var cur_ct = 0;
    var doit = function(){
        let bt = document.createElement('button');
        bt.setAttribute('class','next-btn next-medium next-btn-normal is-wind');
        bt.innerText = 'Del Old Images';
        bt.setAttribute('style','background:red;color:white;');
        bt.addEventListener('click',_=>{
            //let tag_eles = document.querySelectorAll('.next-table-body>tr');
            let min = parseInt(window.prompt('How many versions to keep'));
            cur_ct = min;
            hithit();
        },false);
        document.querySelector('.next-table-header tr:first-of-type>th:last-of-type').appendChild(bt);
    }
    var hithit = function(){
        let modals_button = document.querySelectorAll('[aria-modal="true"][role="alertdialog"] .next-dialog-footer .next-btn-primary.next-dialog-btn');
        if(modals_button){
            for(let mb of modals_button){
              mb.click();
            }
        }
        let tag_eles = document.querySelectorAll('tr[role="row"]');
        let cur = tag_eles[cur_ct];
        let stat = cur.querySelector('.statusFilter').innerText;
        if(stat=='正常'){
            cur.querySelectorAll('.sc-iRbamj')[1].click();
            retryCheck(_=>{
              let lst = document.querySelector('.next-message-content .next-checkbox');
              return lst;
            },500,_=>{
                document.querySelector('.next-message-content .next-checkbox').click();
                document.querySelector('button[class="next-btn next-medium next-btn-primary is-wind"]').click();
                cur_ct++;
                if(cur_ct<tag_eles.length)window.setTimeout(hithit, 1500);
            });
        }else{
            cur_ct++;
            if(cur_ct<tag_eles.length)hithit();
        }
    }
    window.addEventListener('load',_=>{
        retryCheck(_=>{
              let lst = document.querySelector('.next-table-header tr:first-of-type>th:last-of-type');
              return lst;
           },500,_=>{
            doit();
          });
    },false);
})();