微软CRX下载器

使非Edge浏览器,也能从微软扩展商店下载CRX文件

目前為 2021-12-03 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         微软CRX下载器
// @namespace    http://tampermonkey.net/
// @version      0.17
// @description  使非Edge浏览器,也能从微软扩展商店下载CRX文件
// @author       那年那tu那些事
// @include      *://*.microsoft.com/*
// @icon         https://i.loli.net/2021/08/18/mMtybsTwCBkFPW5.png
// @license      MIT
// ==/UserScript==
(function() {
	//获取UA类型
	function getUA() {
		var uaStr = "pc";
		if (/Chrome/i.test(navigator.userAgent)) {
			uaStr = "chromium";
		}
		if (/Android|webOS|iPhone|iPod|BlackBerry|HarmonyOS|Mobile/i.test(navigator.userAgent)) {
			uaStr = "mobile";
		}
		return uaStr;
	}
	//判断是否为detail页
	function checkURL() {
		var URLType;
		if (location.hostname === "microsoftedge.microsoft.com") {
			if (location.pathname.search("/detail") !== -1) {
				URLType = "detail";
			} else {
				URLType = "others";
			}
		} else if (location.hostname === "edge.microsoft.com") {
			if (location.pathname === "/extensionwebstorebase/v1/crx") {
				URLType = "download";
			}
		}
		return URLType;
	}
	//changeButton方法用于替换原图标
	function changeButton(crxid, buttonOBJ) {
		//crxid写入下载器URL
		var downloaderURL =
			"https://edge.microsoft.com/extensionwebstorebase/v1/crx?response=redirect&acceptformat=crx3&x=id%3D" +
			crxid + "%26installsource%3Dondemand%26uc";
		if (getUA() === "chromium") {
			downloaderURL = downloaderURL.replace("https", "http");
		}
		//替换内容、样式、功能
		var buttonOBJtextClass = buttonOBJ.children[0].className;
		buttonOBJ.style =
			"min-width: 100px !important;opacity:1;z-index: 999;cursor:pointer !important;margin-inline-end: 8px;background:#1683D8;";
		buttonOBJ.innerHTML = "<a class=" + buttonOBJtextClass + " href=" + downloaderURL +
			" target='_blank' style='color: white;text-decoration:none;width:100%'><b>下载 CRX</b></a>";
		//解除button的禁用
		buttonOBJ.disabled = false;
		//被替换图标加上name属性,防止一直被遍历赋值
		buttonOBJ.name = "ISnewButton";
		buttonOBJ.onmouseover = function() {
			buttonOBJ.style.background = "#006CBE";
		}
		buttonOBJ.onmouseleave = function() {
			buttonOBJ.style.background = "#1683D8";
		}
		//detail页专属设置
		if (checkURL() === "detail") {
			//图标宽度设置
			buttonOBJ.style.width = "max-content";
			//隐藏原有polite
			var politePareOBJ = buttonOBJ.parentElement.children;
			for (let i = 0; i < politePareOBJ.length; i++) {
				if (politePareOBJ[i].getAttribute("aria-live") === "polite") {
					politePareOBJ[i].style.display = "none";
				}
			}
		}
	}
	//SearchCRXbutton方法用于搜索需要替换的原图标
	function SearchCRXbutton() {
		//获取所有button对象
		var AllButtonObj = document.getElementsByTagName("button");
		//遍历所有button,找到“获取”图标(button id:getOrRemoveButton-xxxxxx)
		for (let i = 0; i < AllButtonObj.length; i++) {
			var ButtonObj = AllButtonObj[i];
			var ButtonObjID = ButtonObj.id;
			var ButtonObjName = ButtonObj.name;
			if ((ButtonObjID.search("getOrRemoveButton-") !== -1) && (ButtonObjName !== "ISnewButton")) {
				//从button id中获取crxid
				var crxid = ButtonObjID.slice("getOrRemoveButton-".length);
				//crxid一般为32位
				crxid = crxid.slice(0, 32);
				//调用方法替换原图标
				changeButton(crxid, ButtonObj);
			}
		}
	}
	//DownloadCRXforChromium方法为chromium内核浏览器专属下载方法
	function DownloadCRXforChromium() {
		var url = location.href;
		var flag=false;
		if (url.search("http://") !== -1) {
			url = url.replace("http", "https");
		} else if (url.search("https://") !== -1) {
			url = "";
		} else {
			url = "https://" + url;
		}
		if (url !== "") {
			var a=document.createElement("a");
			a.href=url;
			a.target="_self";
			a.click();
		}
		setTimeout(function(){//防止还没弹出下载框就被关闭了
			window.close();
		},1000);
	}
	//CRXdownloaderMain方法为脚本入口main
	function CRXdownloaderMain() {
		if (checkURL() === "download" && getUA() === "chromium") {
			DownloadCRXforChromium();
		} else {
			if (document.getElementById("headerArea") !== null) {
				document.getElementById("headerArea").style.zIndex = "1000";
			}
			//设置定时器,单位ms
			var crxdownloaderSetTimer = 100;
			//启动定时器
			var crxdownloaderTimer = setInterval(function() {
				SearchCRXbutton();
			}, crxdownloaderSetTimer);
			//控制台输出定时器ID,可随时通过控制台停止
			console.log(crxdownloaderSetTimer + "ms定时器ID:" + crxdownloaderTimer);
		}
	}
	//调用Main方法
	CRXdownloaderMain(); //判断是否执行
})();