Skip to content

Commit

Permalink
feat: 测试新功能
Browse files Browse the repository at this point in the history
  • Loading branch information
dlzmoe committed Dec 26, 2024
1 parent 5032d0d commit 26ad294
Show file tree
Hide file tree
Showing 19 changed files with 25,651 additions and 157 deletions.
1 change: 0 additions & 1 deletion .output/chrome-mv3/assets/popup-5DI6f4o3.css

This file was deleted.

301 changes: 300 additions & 1 deletion .output/chrome-mv3/background.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,301 @@
var background=function(){"use strict";var e,t;function i(n){return n==null||typeof n=="function"?{main:n}:n}const s=((t=(e=globalThis.browser)==null?void 0:e.runtime)==null?void 0:t.id)==null?globalThis.chrome:globalThis.browser,u=i(()=>{console.log("Hello background!",{id:s.runtime.id})});function l(){}function o(n,...b){}const c={debug:(...n)=>o(console.debug,...n),log:(...n)=>o(console.log,...n),warn:(...n)=>o(console.warn,...n),error:(...n)=>o(console.error,...n)};let r;try{r=u.main(),r instanceof Promise&&console.warn("The background's main() function return a promise, but it must be synchronous")}catch(n){throw c.error("The background crashed on startup!"),n}return r}();
var background = function() {
"use strict";
var _a, _b;
function defineBackground(arg) {
if (arg == null || typeof arg === "function") return { main: arg };
return arg;
}
var _MatchPattern = class {
constructor(matchPattern) {
if (matchPattern === "<all_urls>") {
this.isAllUrls = true;
this.protocolMatches = [..._MatchPattern.PROTOCOLS];
this.hostnameMatch = "*";
this.pathnameMatch = "*";
} else {
const groups = /(.*):\/\/(.*?)(\/.*)/.exec(matchPattern);
if (groups == null)
throw new InvalidMatchPattern(matchPattern, "Incorrect format");
const [_, protocol, hostname, pathname] = groups;
validateProtocol(matchPattern, protocol);
validateHostname(matchPattern, hostname);
this.protocolMatches = protocol === "*" ? ["http", "https"] : [protocol];
this.hostnameMatch = hostname;
this.pathnameMatch = pathname;
}
}
includes(url) {
if (this.isAllUrls)
return true;
const u = typeof url === "string" ? new URL(url) : url instanceof Location ? new URL(url.href) : url;
return !!this.protocolMatches.find((protocol) => {
if (protocol === "http")
return this.isHttpMatch(u);
if (protocol === "https")
return this.isHttpsMatch(u);
if (protocol === "file")
return this.isFileMatch(u);
if (protocol === "ftp")
return this.isFtpMatch(u);
if (protocol === "urn")
return this.isUrnMatch(u);
});
}
isHttpMatch(url) {
return url.protocol === "http:" && this.isHostPathMatch(url);
}
isHttpsMatch(url) {
return url.protocol === "https:" && this.isHostPathMatch(url);
}
isHostPathMatch(url) {
if (!this.hostnameMatch || !this.pathnameMatch)
return false;
const hostnameMatchRegexs = [
this.convertPatternToRegex(this.hostnameMatch),
this.convertPatternToRegex(this.hostnameMatch.replace(/^\*\./, ""))
];
const pathnameMatchRegex = this.convertPatternToRegex(this.pathnameMatch);
return !!hostnameMatchRegexs.find((regex) => regex.test(url.hostname)) && pathnameMatchRegex.test(url.pathname);
}
isFileMatch(url) {
throw Error("Not implemented: file:// pattern matching. Open a PR to add support");
}
isFtpMatch(url) {
throw Error("Not implemented: ftp:// pattern matching. Open a PR to add support");
}
isUrnMatch(url) {
throw Error("Not implemented: urn:// pattern matching. Open a PR to add support");
}
convertPatternToRegex(pattern) {
const escaped = this.escapeForRegex(pattern);
const starsReplaced = escaped.replace(/\\\*/g, ".*");
return RegExp(`^${starsReplaced}$`);
}
escapeForRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
};
var MatchPattern = _MatchPattern;
MatchPattern.PROTOCOLS = ["http", "https", "file", "ftp", "urn"];
var InvalidMatchPattern = class extends Error {
constructor(matchPattern, reason) {
super(`Invalid match pattern "${matchPattern}": ${reason}`);
}
};
function validateProtocol(matchPattern, protocol) {
if (!MatchPattern.PROTOCOLS.includes(protocol) && protocol !== "*")
throw new InvalidMatchPattern(
matchPattern,
`${protocol} not a valid protocol (${MatchPattern.PROTOCOLS.join(", ")})`
);
}
function validateHostname(matchPattern, hostname) {
if (hostname.includes(":"))
throw new InvalidMatchPattern(matchPattern, `Hostname cannot include a port`);
if (hostname.includes("*") && hostname.length > 1 && !hostname.startsWith("*."))
throw new InvalidMatchPattern(
matchPattern,
`If using a wildcard (*), it must go at the start of the hostname`
);
}
const browser = (
// @ts-expect-error
((_b = (_a = globalThis.browser) == null ? void 0 : _a.runtime) == null ? void 0 : _b.id) == null ? globalThis.chrome : (
// @ts-expect-error
globalThis.browser
)
);
const definition = defineBackground(() => {
console.log("Hello background!", { id: browser.runtime.id });
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "sendData") {
chrome.tabs.query({ url: "*://linux.do/*" }, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, {
action: "setData",
data: request.data
});
});
});
}
});
background;
function initPlugins() {
}
function print(method, ...args) {
if (typeof args[0] === "string") {
const message = args.shift();
method(`[wxt] ${message}`, ...args);
} else {
method("[wxt]", ...args);
}
}
const logger = {
debug: (...args) => print(console.debug, ...args),
log: (...args) => print(console.log, ...args),
warn: (...args) => print(console.warn, ...args),
error: (...args) => print(console.error, ...args)
};
let ws;
function getDevServerWebSocket() {
if (ws == null) {
const serverUrl = `${"ws:"}//${"localhost"}:${3e3}`;
logger.debug("Connecting to dev server @", serverUrl);
ws = new WebSocket(serverUrl, "vite-hmr");
ws.addWxtEventListener = ws.addEventListener.bind(ws);
ws.sendCustom = (event, payload) => ws == null ? void 0 : ws.send(JSON.stringify({ type: "custom", event, payload }));
ws.addEventListener("open", () => {
logger.debug("Connected to dev server");
});
ws.addEventListener("close", () => {
logger.debug("Disconnected from dev server");
});
ws.addEventListener("error", (event) => {
logger.error("Failed to connect to dev server", event);
});
ws.addEventListener("message", (e) => {
try {
const message = JSON.parse(e.data);
if (message.type === "custom") {
ws == null ? void 0 : ws.dispatchEvent(
new CustomEvent(message.event, { detail: message.data })
);
}
} catch (err) {
logger.error("Failed to handle message", err);
}
});
}
return ws;
}
function keepServiceWorkerAlive() {
setInterval(async () => {
await browser.runtime.getPlatformInfo();
}, 5e3);
}
function reloadContentScript(payload) {
const manifest = browser.runtime.getManifest();
if (manifest.manifest_version == 2) {
void reloadContentScriptMv2();
} else {
void reloadContentScriptMv3(payload);
}
}
async function reloadContentScriptMv3({
registration,
contentScript
}) {
if (registration === "runtime") {
await reloadRuntimeContentScriptMv3(contentScript);
} else {
await reloadManifestContentScriptMv3(contentScript);
}
}
async function reloadManifestContentScriptMv3(contentScript) {
const id = `wxt:${contentScript.js[0]}`;
logger.log("Reloading content script:", contentScript);
const registered = await browser.scripting.getRegisteredContentScripts();
logger.debug("Existing scripts:", registered);
const existing = registered.find((cs) => cs.id === id);
if (existing) {
logger.debug("Updating content script", existing);
await browser.scripting.updateContentScripts([{ ...contentScript, id }]);
} else {
logger.debug("Registering new content script...");
await browser.scripting.registerContentScripts([{ ...contentScript, id }]);
}
await reloadTabsForContentScript(contentScript);
}
async function reloadRuntimeContentScriptMv3(contentScript) {
logger.log("Reloading content script:", contentScript);
const registered = await browser.scripting.getRegisteredContentScripts();
logger.debug("Existing scripts:", registered);
const matches = registered.filter((cs) => {
var _a2, _b2;
const hasJs = (_a2 = contentScript.js) == null ? void 0 : _a2.find((js) => {
var _a3;
return (_a3 = cs.js) == null ? void 0 : _a3.includes(js);
});
const hasCss = (_b2 = contentScript.css) == null ? void 0 : _b2.find((css) => {
var _a3;
return (_a3 = cs.css) == null ? void 0 : _a3.includes(css);
});
return hasJs || hasCss;
});
if (matches.length === 0) {
logger.log(
"Content script is not registered yet, nothing to reload",
contentScript
);
return;
}
await browser.scripting.updateContentScripts(matches);
await reloadTabsForContentScript(contentScript);
}
async function reloadTabsForContentScript(contentScript) {
const allTabs = await browser.tabs.query({});
const matchPatterns = contentScript.matches.map(
(match) => new MatchPattern(match)
);
const matchingTabs = allTabs.filter((tab) => {
const url = tab.url;
if (!url)
return false;
return !!matchPatterns.find((pattern) => pattern.includes(url));
});
await Promise.all(
matchingTabs.map(async (tab) => {
try {
await browser.tabs.reload(tab.id);
} catch (err) {
logger.warn("Failed to reload tab:", err);
}
})
);
}
async function reloadContentScriptMv2(_payload) {
throw Error("TODO: reloadContentScriptMv2");
}
{
try {
const ws2 = getDevServerWebSocket();
ws2.addWxtEventListener("wxt:reload-extension", () => {
browser.runtime.reload();
});
ws2.addWxtEventListener("wxt:reload-content-script", (event) => {
reloadContentScript(event.detail);
});
if (true) {
ws2.addEventListener(
"open",
() => ws2.sendCustom("wxt:background-initialized")
);
keepServiceWorkerAlive();
}
} catch (err) {
logger.error("Failed to setup web socket connection with dev server", err);
}
browser.commands.onCommand.addListener((command) => {
if (command === "wxt:reload-extension") {
browser.runtime.reload();
}
});
}
let result;
try {
initPlugins();
result = definition.main();
if (result instanceof Promise) {
console.warn(
"The background's main() function return a promise, but it must be synchronous"
);
}
} catch (err) {
logger.error("The background crashed on startup!");
throw err;
}
const result$1 = result;
return result$1;
}();
background;
15 changes: 15 additions & 0 deletions .output/chrome-mv3/bookmark.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="module" src="http://localhost:3000/@vite/client"></script>
<script src="http://localhost:3000/@id/virtual:wxt-html-plugins" type="module"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LinuxDo Scripts 扩展设置</title>
<meta name="manifest.type" content="browser_action"> <script type="module" crossorigin="" src="/chunks/reload-html-BxzUToPe.js"></script>
</head>
<body>
<div id="app"></div>
<script type="module" src="http://localhost:3000/entrypoints/bookmark/main.ts"></script>
</body>
</html>
17 changes: 0 additions & 17 deletions .output/chrome-mv3/chunks/popup-y6NwxbGV.js

This file was deleted.

Loading

0 comments on commit 26ad294

Please sign in to comment.