Browse Source

初次提交

杨东明 9 tháng trước cách đây
commit
d6e64156d0
5 tập tin đã thay đổi với 230 bổ sung0 xóa
  1. 4 0
      background.js
  2. 119 0
      content.js
  3. 19 0
      manifest.json
  4. 67 0
      popup.html
  5. 21 0
      popup.js

+ 4 - 0
background.js

@@ -0,0 +1,4 @@
+// background.js
+chrome.runtime.onInstalled.addListener(function () {
+  console.log("插件已被安装");
+});

+ 119 - 0
content.js

@@ -0,0 +1,119 @@
+let overDiv;
+chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
+  if (request.action === "highlight") {
+    if (request.message && !isOpen) {
+      overDiv = document.createElement("div");
+      overDiv.classList.add("bg-overDiv");
+      // CSS样式内容
+      var css = `
+        .bg-overDiv{
+            position: fixed;
+            opacity: 0.5;
+            background: rgb(20 122 218 / 85%);
+            backdrop-filter: blur(5px);
+            cursor: pointer;
+            z-index: 99999999999;
+            display: none;
+        }`;
+
+      // 创建一个style元素
+      var style = document.createElement("style");
+      style.classList.add("bg-style");
+
+      // 为style元素添加CSS内容
+      if (style.styleSheet) {
+        style.styleSheet.cssText = css;
+      } else {
+        style.appendChild(document.createTextNode(css));
+      }
+
+      // 将style元素添加到body的末尾
+      document.body.appendChild(style); //把这个样式加入body的最后
+
+      document.body.appendChild(overDiv);
+      isOpen = true;
+
+      window.addEventListener("click", clickHandler);
+      window.addEventListener("mousemove", mouseMoveHandler);
+    }
+
+    if (!request.message && isOpen) {
+      document.querySelector(".bg-overDiv")?.remove();
+      document.querySelector(".bg-style")?.remove();
+      window.removeEventListener("click", clickHandler);
+      window.removeEventListener("mousemove", mouseMoveHandler);
+      isOpen = false;
+    }
+  }
+});
+
+let isOpen = false;
+
+function clickHandler(e) {
+  console.log(oldTarget);
+}
+
+// window.addEventListener("click", function (e) {
+//   // e.preventDefault();
+//   // e.stopPropagation();
+//   console.log("点击");
+// });
+let oldTarget;
+
+function mouseMoveHandler(e) {
+  if (!isOpen) return;
+  let target =
+    findTarget(document.elementsFromPoint(e.clientX, e.clientY)) || e.target;
+  if (oldTarget !== target) {
+    overDiv.style.cssText = "";
+    oldTarget = target;
+    let rect = target.getClientRects();
+    if (rect.length > 0) {
+      rect = rect[0];
+      overDiv.style.cssText =
+        "display: block; top:" +
+        rect.top +
+        "px;left: " +
+        rect.left +
+        "px; width: " +
+        rect.width +
+        "px; height: " +
+        rect.height +
+        "px;";
+    }
+  }
+  oldTarget = target;
+  let oldEle = document.querySelector(".bg-overlay");
+  if (oldEle) {
+    oldEle.classList.remove("bg-overlay");
+  }
+  target.classList.add("bg-overlay");
+}
+
+function findTarget(eles) {
+  for (let i = 0; i < eles.length; i++) {
+    if (eles[i].classList.contains("bg-overDiv") === false) return eles[i];
+  }
+}
+function findChildren(ele, x, y) {
+  let clds = ele.children;
+  let findEle = ele;
+  if (clds.length > 0) {
+    let len = clds.length;
+    let cur = null;
+    for (let i = 0; i < len; i++) {
+      let rect = clds[i].getClientRects();
+      if (!rect[0]) continue;
+      if (
+        rect[0].x + rect[0].width >= x &&
+        rect[0].x <= x &&
+        rect[0].y + rect[0].height >= y &&
+        rect[0].y <= y
+      ) {
+        findEle = findChildren(clds[i], x, y);
+        break;
+      }
+    }
+  }
+  return findEle;
+}

+ 19 - 0
manifest.json

@@ -0,0 +1,19 @@
+{
+  "name": "chrome-plugin",
+  "version": "1.0",
+  "description": "Build an Extension!",
+  "manifest_version": 3,
+  "permissions": ["activeTab", "storage"],
+  "action": {
+    "default_popup": "popup.html"
+  },
+  "background": {
+    "service_worker": "background.js"
+  },
+  "content_scripts": [
+    {
+      "matches": ["<all_urls>"],
+      "js": ["content.js"]
+    }
+  ]
+}

+ 67 - 0
popup.html

@@ -0,0 +1,67 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <title>Document</title>
+    <style>
+      .switch-an {
+        position: absolute;
+        clip: rect(0, 0, 0, 0);
+      }
+
+      .switch-an[type="checkbox"] + label {
+        position: relative;
+        display: inline-block;
+        width: 60px;
+        height: 26px;
+        border-radius: 1em;
+        color: #fff;
+        background: #a5a5a5;
+        text-align: left;
+        font-size: 15px;
+        cursor: pointer;
+      }
+
+      .switch-an[type="checkbox"] + label::before {
+        content: "";
+        width: 24px;
+        height: 24px;
+        top: 1px;
+        position: absolute;
+        left: 2px;
+        border-radius: 100%;
+        vertical-align: middle;
+        background-color: #fff;
+        transition: left 0.2s;
+      }
+
+      .switch-an[type="checkbox"] + label::after {
+        content: "";
+        margin-left: 37px;
+        position: relative;
+        top: 2px;
+      }
+      .switch-an[type="checkbox"]:checked + label {
+        background-color: #06c;
+      }
+      .switch-an[type="checkbox"]:checked + label::before {
+        transition: left 0.2s;
+        left: 34px;
+      }
+
+      .switch-an[type="checkbox"]:checked + label::after {
+        content: "";
+        margin-left: 11px;
+      }
+    </style>
+  </head>
+  <body>
+    <div class="check-wrap">
+      <input type="checkbox" class="switch-an" id="highlight-switch" />
+      <label for="highlight-switch"></label>
+    </div>
+    <!-- <button id="highlight-button">选择元素并高亮</button> -->
+    <script src="popup.js"></script>
+  </body>
+</html>

+ 21 - 0
popup.js

@@ -0,0 +1,21 @@
+// 加载开关状态
+chrome.storage.sync.get(["highlightEnabled"], (result) => {
+  let isChecked = result.highlightEnabled || false;
+  document.getElementById("highlight-switch").checked = isChecked;
+  sendMessage(isChecked);
+});
+
+document.getElementById("highlight-switch").addEventListener("change", (e) => {
+  let isChecked = e.target.checked;
+  chrome.storage.sync.set({ highlightEnabled: isChecked });
+  sendMessage(isChecked);
+});
+
+function sendMessage(isChecked) {
+  chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
+    chrome.tabs.sendMessage(tabs[0].id, {
+      action: "highlight",
+      message: isChecked,
+    });
+  });
+}