|
| 1 | +import { message } from "antd"; |
| 2 | +// import "./content.css"; |
| 3 | +import { copyToClipboard } from "./utils"; |
| 4 | + |
| 5 | +// testcase eg: `bottom = "BCD", allowed = ["BCG", "CDE", "GEA", "FFF"], c = [1,2,3], d = 2` |
| 6 | +function normalize(testCase) { |
| 7 | + testCase = testCase.replace(/\n/g, "").replace(" ", ""); |
| 8 | + console.log(testCase); |
| 9 | + // 单一参数 |
| 10 | + if (!testCase.includes("=")) { |
| 11 | + // 数组直接返回 |
| 12 | + if (testCase.includes("[")) { |
| 13 | + return testCase; |
| 14 | + } else { |
| 15 | + // 输入: 3, 2, 0, 0 |
| 16 | + // 输入: 0.0625 |
| 17 | + |
| 18 | + const parts = testCase.split(","); |
| 19 | + if (parts.length == 0) return parts.join(""); |
| 20 | + return parts.join("\n"); |
| 21 | + } |
| 22 | + } |
| 23 | + let stack = []; |
| 24 | + let i = 0; |
| 25 | + while (i < testCase.length) { |
| 26 | + while (i < testCase.length && testCase[i] !== "=") { |
| 27 | + i += 1; |
| 28 | + } |
| 29 | + // skip = |
| 30 | + i += 1; |
| 31 | + |
| 32 | + while (i < testCase.length && testCase[i] !== "[" && testCase[i] != ",") { |
| 33 | + stack.push(testCase[i]); |
| 34 | + i += 1; |
| 35 | + } |
| 36 | + if (testCase[i] == ",") { |
| 37 | + // skip , |
| 38 | + i += 1; |
| 39 | + stack.push("\n"); |
| 40 | + } else { |
| 41 | + // cnt 左括号[ 与 右括号] 个数的差值 |
| 42 | + let cnt = 0; |
| 43 | + while (i < testCase.length) { |
| 44 | + stack.push(testCase[i]); |
| 45 | + cnt += testCase[i] === "["; |
| 46 | + cnt -= testCase[i] === "]"; |
| 47 | + i += 1; |
| 48 | + if (cnt == 0) { |
| 49 | + if (i !== testCase.length) { |
| 50 | + stack.push("\n"); |
| 51 | + } |
| 52 | + |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return stack.join(""); |
| 59 | +} |
| 60 | + |
| 61 | +function getProviedTestCases() { |
| 62 | + const possibleTags = ["pre", "p"]; |
| 63 | + const possiblePrefixs = ["输入:", "输入:"]; |
| 64 | + const ans = []; |
| 65 | + for (let tag of possibleTags) { |
| 66 | + const pres = document.querySelectorAll(tag); |
| 67 | + |
| 68 | + for (let prefix of possiblePrefixs) { |
| 69 | + for (var i = 0; i < pres.length; ++i) { |
| 70 | + if (pres[i].innerText.includes(prefix)) { |
| 71 | + console.log( |
| 72 | + pres[i].innerText.match(new RegExp(`${prefix}(.*)输出`, "s")) |
| 73 | + ); |
| 74 | + const testcase = pres[i].innerText.match( |
| 75 | + new RegExp(`${prefix}(.*?)输出`, "mgs") |
| 76 | + )[1]; |
| 77 | + ans.push(normalize(testcase)); |
| 78 | + } |
| 79 | + } |
| 80 | + if (ans.length > 0) return ans; |
| 81 | + } |
| 82 | + } |
| 83 | + return ans; |
| 84 | +} |
| 85 | + |
| 86 | +function insertButton() { |
| 87 | + const buttons = document.querySelectorAll("button"); |
| 88 | + for (var i = 0; i < buttons.length; ++i) { |
| 89 | + if (buttons[i].innerText.includes("执行代码")) { |
| 90 | + const copyButton = buttons[i].cloneNode(true); |
| 91 | + copyButton.innerText = "复制所有内置用例"; |
| 92 | + copyButton.style["margin-left"] = "10px"; |
| 93 | + copyButton.onclick = () => { |
| 94 | + const cases = getProviedTestCases(); |
| 95 | + if (cases.filter(Boolean).length === 0) |
| 96 | + return message.error({ |
| 97 | + content: |
| 98 | + "力扣不讲武德,不套路出牌。不过没关系啊,你反馈给我,我下次一定全部防出去啊!", |
| 99 | + }); |
| 100 | + copyToClipboard(cases.join("\n")); |
| 101 | + message.success({ |
| 102 | + content: "复制成功~", |
| 103 | + }); |
| 104 | + }; |
| 105 | + buttons[i].parentElement.prepend(copyButton); |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +let inserted = false; |
| 111 | +const timerId = setInterval(() => { |
| 112 | + if (inserted) return clearInterval(timerId); |
| 113 | + insertButton(); |
| 114 | + inserted = true; |
| 115 | +}, 1000); |
| 116 | + |
| 117 | +// class Main extends React.Component { |
| 118 | +// render() { |
| 119 | +// return ( |
| 120 | +// <div className={"my-extension"}> |
| 121 | +// <h1 onClick={t}>Hello world - My first Extension</h1> |
| 122 | +// </div> |
| 123 | +// ); |
| 124 | +// } |
| 125 | +// } |
| 126 | + |
| 127 | +// const app = document.createElement("div"); |
| 128 | +// app.id = "my-extension-root"; |
| 129 | +// document.body.appendChild(app); |
| 130 | + |
| 131 | +// ReactDOM.render(<Main />, app); |
0 commit comments