Skip to content

Commit

Permalink
chore: 修改同步设置
Browse files Browse the repository at this point in the history
  • Loading branch information
dlzmoe committed Jan 3, 2025
1 parent 6899620 commit 581e039
Show file tree
Hide file tree
Showing 14 changed files with 25,458 additions and 357 deletions.
1 change: 0 additions & 1 deletion .output/chrome-mv3/assets/bookmark-UxQstQ-h.css

This file was deleted.

1 change: 0 additions & 1 deletion .output/chrome-mv3/assets/popup-5DI6f4o3.css

This file was deleted.

334 changes: 333 additions & 1 deletion .output/chrome-mv3/background.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,334 @@
var background=function(){"use strict";var t,s;function c(e){return e==null||typeof e=="function"?{main:e}:e}const u=((s=(t=globalThis.browser)==null?void 0:t.runtime)==null?void 0:s.id)==null?globalThis.chrome:globalThis.browser,l=c(()=>{console.log("Hello background!",{id:u.runtime.id})});chrome.runtime.onMessage.addListener((e,i,m)=>{e.action==="sendData"&&chrome.tabs.query({url:"*://linux.do/*"},a=>{a.forEach(r=>{chrome.tabs.sendMessage(r.id,{action:"setData",data:e.data})})})}),chrome.runtime.onMessage.addListener((e,i,m)=>{if(e.action==="open_bookmark_page"){const r=`chrome-extension://${chrome.runtime.id}/bookmark.html`;chrome.tabs.create({url:r})}});function h(){}function n(e,...i){}const d={debug:(...e)=>n(console.debug,...e),log:(...e)=>n(console.log,...e),warn:(...e)=>n(console.warn,...e),error:(...e)=>n(console.error,...e)};let o;try{o=l.main(),o instanceof Promise&&console.warn("The background's main() function return a promise, but it must be synchronous")}catch(e){throw d.error("The background crashed on startup!"),e}return o}();
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
)
);
async function handleWebDAVRequest(auth, method, url, data = null) {
const headers = {
"Authorization": "Basic " + btoa(`${auth.username}:${auth.password}`)
};
if (method === "PUT") {
headers["Content-Type"] = "application/json";
}
try {
const response = await fetch(url, {
method,
headers,
body: method === "PUT" ? data : null
});
const responseData = await response.text();
return { status: response.status, statusText: response.statusText, data: responseData };
} catch (error) {
return { status: error.response ? error.response.status : 500, statusText: error.message };
}
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const { action, url, auth, method, data } = message;
if (action === "webdavRequest") {
handleWebDAVRequest(auth, method, url, data).then((response) => sendResponse(response)).catch((error) => sendResponse({ status: 500, statusText: error.message }));
return true;
}
});
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
});
});
});
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "open_bookmark_page") {
const extensionID = chrome.runtime.id;
const extensionURL = `chrome-extension://${extensionID}/bookmark.html`;
chrome.tabs.create({ url: extensionURL });
}
});
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;
10 changes: 5 additions & 5 deletions .output/chrome-mv3/bookmark.html
Original file line number Diff line number Diff line change
@@ -1,15 +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/bookmark-2YPfUBW8.js"></script>
<link rel="modulepreload" crossorigin href="/chunks/_plugin-vue_export-helper-D2bdTEIC.js">
<link rel="stylesheet" crossorigin href="/assets/bookmark-UxQstQ-h.css">
</head>
<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/_plugin-vue_export-helper-D2bdTEIC.js

This file was deleted.

45 changes: 0 additions & 45 deletions .output/chrome-mv3/chunks/bookmark-2YPfUBW8.js

This file was deleted.

1 change: 0 additions & 1 deletion .output/chrome-mv3/chunks/popup-viRAzd44.js

This file was deleted.

Loading

0 comments on commit 581e039

Please sign in to comment.