Skip to content

Commit

Permalink
fix: correct @match header generation
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep committed Oct 14, 2024
1 parent c580f69 commit ee119c5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
14 changes: 3 additions & 11 deletions src/generators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,9 @@ export const generateMatchHeaders = async <T extends CommonHeaders>(
const match
= matches.find(m => m.includes("domain")) || "https://domain/*";

const sites = await networkSiteScraper();

if (matches.includes("meta")) {
const metaSites = sites.flatMap(({ site, ...rest }) => {
return collapse && (site.includes("stackexchange") || site.includes("stackapps"))
? []
: [{ ...rest, site: site.replace(/^(.+?\.(?=.+\.)|)/, "$1meta.") }];
});

sites.push(...metaSites);
}
const sites = (await networkSiteScraper())
// only inclue meta sites if requested
.filter(({ isMeta }) => matches.includes("meta") || !isMeta);

const all = sites.map(({ site }) => {
const domain
Expand Down
7 changes: 5 additions & 2 deletions src/utils/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ export interface NetworkSiteInfo {
icon: string;
site: string;
name: string;
isMeta: boolean;
}

interface ApiItems {
icon_url: string;
site_url: string;
site_type: "main_site" | "meta_site";
name: string;
}

Expand All @@ -23,7 +25,7 @@ export const scrapeNetworkSites = async () => {
const url = new URL("https://api.stackexchange.com/2.3/sites");
url.searchParams.set("key", "52HhpSRv*u6t*)tOFwEIHw((");
url.searchParams.set("pagesize", "100");
url.searchParams.set("filter", "!SldCuNUOe7I(DQo2T0");
url.searchParams.set("filter", "!SldCsaEs0vkmNDgMK_");

const siteInfo: NetworkSiteInfo[] = [];

Expand All @@ -42,11 +44,12 @@ export const scrapeNetworkSites = async () => {
}

items
.forEach(({ icon_url: icon, site_url: site, name }) => {
.forEach(({ icon_url: icon, site_url: site, name, site_type: siteType }) => {
siteInfo.push({
icon,
site: site.replace(/^https?:\/\//, "").replace(/\/$/, ""),
name,
isMeta: siteType === "meta_site"
});
});

Expand Down
20 changes: 20 additions & 0 deletions test/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ describe("common", () => {
expect(matches).length.to.be.greaterThan(1);
});

it('"match" with "all meta" should expand to all meta sites', async () => {
const headers = await generateMatchHeaders(
["all", "meta", "https://domain/questions/*"],
(async () => [
{ site: "stackoverflow.com" },
{ site: "chess.stackexchange.com" },
{ site: "meta.stackexchange.com" },
{ site: "pt.meta.stackoverflow.com" }
]) as typeof scrapeNetworkSites
);

const matches = headers.map(([, m]) => m);
expect(matches).to.include("https://*.stackexchange.com/questions/*");

expect(matches).to.not.include("https://pt.meta.meta.stackoverflow.com/questions/*");
expect(matches).to.include("https://pt.meta.stackoverflow.com/questions/*");

expect(matches.length).to.equal(3);
});

it('@require headers should be generated', async () => {
const content = await generate("tampermonkey", {
...directCommon,
Expand Down

0 comments on commit ee119c5

Please # to comment.