自定义谷歌插件-扫描openstack官网接口


插件文件结构
./manifest.json
./js/index.js

manifest.json

{
	//插件版本号
	"version":"1.0",
	//插件框架版本号
	"manifest_version":3,
	//插件名称
	"name":"psw_dist",
	//作者名称
	"author":"fenglei",
	//图标
	//"icons": {
	//	"16":"img/icon.png",
	//	"48":"img/icon.png",
	//	"128":"img/icon.png"
	//},
	//申请组件权限
	"permissions": [
		"tabs",
		"contextMenus",
		"activeTab",
		"storage"
	],
	//后台运行脚本
	"content_scripts": [
		{
			"matches":["https://*/*","http://*/*"],
			"js":["js/index.js"]
		}
	]
}

index.js 扫描谷歌接口脚本

(function () {
	var loopTask = {
		loop: "true",
		loopTime: 500,
		loopCount: 0,
		tasks: [
			{
				loop: "true",
				funcName: "one",
			},
		],
	};

	function isEmpty(o) {
		return !o || "" === 0 || null === o || "null" === o || undefined === o || "undefined" === o;
	}

	function getUrl() {
		return window.location.href ? window.location.href : "";
	}

	function setValue(doc, vK, newVal) {
		if (isEmpty(doc) || isEmpty(doc[vK])) {
			return;
		}
		newVal = newVal ? newVal : "";
		doc[vK] = newVal;

		if (typeof (doc.dispatchEvent) === "function") {
			let evt = new Event("input", { bubbles: true });
			evt.simulated = true;
			doc.dispatchEvent(evt);
		}

		if ("value" === vK) {
			let tracker = doc._valueTracker;
			if (tracker) {
				tracker.setValue(newVal);
			}
		}
	}

	function getCacheLoopTask() {
		let web_plugin_loopTask = localStorage.getItem("web_plugin_loopTask");
		if (isEmpty(web_plugin_loopTask)) {
			return undefined;
		}
		if ("string" === typeof web_plugin_loopTask) {
			web_plugin_loopTask = JSON.parse(web_plugin_loopTask);
		}
		if ("object" !== typeof web_plugin_loopTask || isEmpty(web_plugin_loopTask["loop"])) {
			localStorage.removeItem("web_plugin_loopTask");
			web_plugin_loopTask = undefined;
		}
		return web_plugin_loopTask;
	}

	function setLoopTask() {
		let web_plugin_loopTask = getCacheLoopTask();
		if (!isEmpty(web_plugin_loopTask)) {
			web_plugin_loopTask.loopCount = loopTask.loopCount;
			loopTask = web_plugin_loopTask;
		}
		localStorage.setItem("web_plugin_loopTask", JSON.stringify(loopTask));
	}
	setLoopTask();

	function initPageList() {
		let apiReference = Array.from(document.querySelectorAll("a")).filter((x, y, z) => { return String(x.innerText.replaceAll(" ", "")) === "APIReference" });
		if (apiReference.length > 0) {
			localStorage.setItem("apiReferenceIndex", 0);
			let urls = apiReference.map(x => x.href);
			localStorage.setItem("apiReference", JSON.stringify(urls));
			localStorage.removeItem("cacheApiInterface");
			apiReference[0].click();
		}
	}

	function nextPage() {
		initPageList();
		let apiReferenceIndex = Number(localStorage.getItem("apiReferenceIndex"));
		let apiHomeList = JSON.parse(localStorage.getItem("apiReference"));
		apiReferenceIndex++;

		if (apiReferenceIndex >= apiHomeList.length) {
			return;
		}
		localStorage.setItem("apiReferenceIndex", apiReferenceIndex);

		const stepNextPage = setTimeout(function () {
			let nextPage = document.createElement("a");
			nextPage.href = apiHomeList[apiReferenceIndex];
			nextPage.click();
			clearTimeout(stepNextPage);
		}, 2000);
	}

	var indexUrl = document.querySelectorAll("a [class='fa fa-angle-double-right']")[0];
	var apiPath = Array.from(document.querySelectorAll("div [role='main'] section")).filter((x, y, z) => { return String(x.className).indexOf("detail-control") >= 0 || String(x.className).indexOf("api-detail collapse") >= 0 });
	// 路径:detail-control 传参和返回:api-detail.collapse.* 传参:id* 返回:response*

	let apiArr = Array.from([]);

	var taskFunc = {
		one() {
			let apiEntity = {
				url: null,
				urlStr: null,
				env: null,
				envJsonObj: Array.from([]),
			};

			Array.from(document.querySelectorAll("div [role='main'] section")).forEach(x => {
				if (!isEmpty(apiEntity.url) && !isEmpty(apiEntity.env)) {
					apiArr.push(apiEntity);
					apiEntity = {
						url: null,
						urlStr: null,
						env: null,
						envJsonObj: Array.from([]),
					};
				}
				if (String(x.className).indexOf("detail-control") >= 0) {
					if (isEmpty(apiEntity.url)) {
						apiEntity.url = x;
						apiEntity.urlStr = x.getElementsByClassName("endpoint-container")[0].children[0].innerText;
					}
				} else if (String(x.className).indexOf("api-detail collapse") >= 0) {
					if (isEmpty(apiEntity.env)) {
						apiEntity.env = x;
						Array.from(x.getElementsByTagName("section")).forEach(p => {
							let tbName = "";
							let tbTitle = p.getElementsByTagName("h4");
							if (tbTitle.length > 0) {
								tbName = tbTitle[0].innerText;
							}
							let tbs = Array.from(p.getElementsByTagName("table"));
							tbs.forEach(r => {
								let tabTxt = r.innerText;
								let tbHeader = Array.from([]);
								let handCheck = true;
								let spiltLen = 0;
								let nowSpiltLen = 0;
								let cellIndex = 0;
								let nowRow = {};
								let lastStr = "";
								for (let k in tabTxt) {
									let nowStr = tabTxt[k];
									if (handCheck) {
										if ("\n" === nowStr) {
											if (lastStr !== "" && lastStr !== "\n") {
												nowSpiltLen = 0;
												cellIndex++;
											}
											if (cellIndex == 1) {
												spiltLen++;
											} else if (cellIndex > 1) {
												nowSpiltLen++;
												if (nowSpiltLen > spiltLen) {
													handCheck = false;
													cellIndex = 0;
													nowSpiltLen = 0;
												}
											}
										} else {
											if (tbHeader.length < (cellIndex + 1)) {
												tbHeader.push(nowStr);
											} else {
												tbHeader[cellIndex] = tbHeader[cellIndex] + nowStr;
											}
										}
									} else {
										if ("\n" === nowStr) {
											if (lastStr !== "" && lastStr !== "\n") {
												nowSpiltLen = 0;
												cellIndex++;
											}
											if (cellIndex > 1) {
												nowSpiltLen++;
												if (nowSpiltLen > spiltLen) {
													cellIndex = 0;
													apiEntity.envJsonObj.push({ name: tbName, jsonObj: nowRow });
													nowRow = {};
												}
											}
										} else {
											var hk = tbHeader[cellIndex];
											if (isEmpty(nowRow[hk])) {
												nowRow[hk] = nowStr;
											} else {
												nowRow[hk] = nowRow[hk] + nowStr;
											}
										}
									}
									lastStr = nowStr;
								}
							});
							if (tbs.length < 1) {
								Array.from(p.getElementsByClassName("highlight")).forEach(r => {
									try {
										let nowRow = JSON.parse(r.innerText);
										if (isEmpty(nowRow)) {
											console.log("@", nowRow);
										}
										apiEntity.envJsonObj.push({ name: tbName, jsonObj: nowRow });
									} catch (error) {
										console.log("@error json:", r.innerText, r, p);
										apiEntity.envJsonObj.push({ name: tbName, jsonObj: { data: r.innerText } });
									}
								});
							}
						});
					}
				}
			});
			let apiInterface = apiArr.map(x => { return { url: x.urlStr, env: x.envJsonObj } });
			let cacheApiInterface = localStorage.getItem("cacheApiInterface");
			if (isEmpty(cacheApiInterface)) {
				cacheApiInterface = apiInterface;
			} else {
				cacheApiInterface = JSON.parse(cacheApiInterface);
				cacheApiInterface = cacheApiInterface.concat(apiInterface);
			}
			localStorage.setItem("cacheApiInterface", JSON.stringify(cacheApiInterface));
			nextPage();
		},

	}

	function startTask() {
		if (!isEmpty(window["cache_inv"])) {
			clearInterval(window["cache_inv"]);
		}
		let taskExec = function () {
			for (let k in loopTask.tasks) {
				let task = loopTask.tasks[k];
				if ("true" === String(task["loop"])) {
					taskFunc[task.funcName]();
					loopTask.loopCount++;
				}
			}
			setLoopTask();
		}
		if ("true" === String(loopTask["loop"])) {
			window["cache_inv"] = setInterval(function () {
				taskExec();
				if (!"true" === String(loopTask["loop"])) {
					clearInterval(window["cache_inv"]);
				}
			}, loopTask.loopTime);
		} else {
			taskExec();
		}
	}

	function startPlugin() {
		startTask();
		setInterval(function () {
			let cacheLoopTask = getCacheLoopTask();
			if (!isEmpty(cacheLoopTask)) {
				if (String(cacheLoopTask["loop"]) !== String(loopTask["loop"])) {
					loopTask["loop"] = cacheLoopTask["loop"]
					if ("true" === String(loopTask["loop"])) {
						console.log("@start loopTask:", loopTask);
						startTask();
					} else {
						startTask();
						console.log("@stop  loopTask:", loopTask);
					}
				}
			} else {
				setLoopTask();
			}
		}, 500);
	}

	window.onload = function () {
		// console.clear();
		startPlugin();
	}
})();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值