Console Importer

通过控制台导入 JS / CSS 库

目前為 2023-09-17 提交的版本,檢視 最新版本

// ==UserScript==
// @name		Console Importer
// @name:en		Console Importer
// @description		通过控制台导入 JS / CSS 库
// @description:en		Import JavaScript or CSS Library in browser developer tool.
// @author		Yiero
// @version		1.0.0
// @match		https://*/*
// @run-at      document-body
// @license		GPL
// @icon		https://bbs.tampermonkey.net.cn/favicon.ico
// @grant		GM_addElement
// @grant		GM_xmlhttpRequest
// @namespace		https://github.com/AliubYiero/TamperMonkeyScripts/
// @connect		cdnjs.cloudflare.com
// @connect		cdn.bootcdn.net
// @connect		cdn.jsdelivr.net
// ==/UserScript==
function isCSS( url ) {
	return url.endsWith( "css" );
}

const urlList = new Map;

function getRequire( url ) {
	if ( urlList.has( url ) ) {
		console.error( "[%s] \u83b7\u53d6\u6570\u636e\u5931\u8d25...\n\u5f53\u524d\u9875\u9762\u4e2d\u5df2\u5b58\u5728\u5e93 [%s]", "Console Importer", url );
		return;
	}
	GM_xmlhttpRequest( {
		method: "GET",
		url: url,
		onload( e ) {
			const scriptText = e.responseText;
			const isScript = isCSS( url );
			const element = GM_addElement( document.head, isScript ? "script" : "style", {
				textContent: scriptText
			} );
			urlList.set( url, element.id );
		},
		onerror( e ) {
			console.error( "[%s] \u83b7\u53d6\u6570\u636e\u5931\u8d25...\n", "Console Importer", e );
		}
	} );
}

( () => {
	unsafeWindow.$i = getRequire;
} )();