Skip to content

Commit

Permalink
Add url fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
ZipFile committed May 8, 2021
1 parent 53c357a commit 04827b4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ export const queryCodes = {
],
};

export function regexFixup(matchURLRegex, ...args) {
return {
match(url) {
return matchURLRegex.test(url);
},
fix(url) {
for (let [match, replaceWith] of args) {
url = url.replace(match, replaceWith);
}
return url
},
};
}

export const urlFixups = [
regexFixup(/\.hdslb\.com\//, [/@.*/, ""]),
regexFixup(/:\/\/media.discordapp.net\//, [/\?.*/, ""]),
regexFixup(/\.pinimg\.com\//, [/\/\d+x\//, "/originals/"]),
regexFixup(/(pixiv|booth)\.pximg\.net\//, [/\/c\/\d+x\d+.*?\//, "/"], [/_base_resized/, ""]),
regexFixup(/:\/\/c\.fantia.jp\//, [/(\d+)\/.*?_/, "$1/"]),
];

export function fixUrl(url) {
for (let fixup of urlFixups) {
if (fixup.match(url)) {
url = fixup.fix(url);
}
}
return url;
}

export class TabUtils {
constructor(tab, api) {
this.tab = tab;
Expand Down Expand Up @@ -171,6 +202,8 @@ export function makeBatchUrl(prefix, url) {
export function makePostUrl(prefix, url, ref) {
const uploadUrl = new URL("uploads/new", prefix);



uploadUrl.searchParams.set("url", url);

if (ref) {
Expand Down Expand Up @@ -203,7 +236,7 @@ export async function makeUrl(prefix, batch, info, getReferrer) {
ref = info.pageUrl;
}

return makePostUrl(prefix, info.srcUrl, ref);
return makePostUrl(prefix, fixUrl(info.srcUrl), ref);
}

export function getPageActionMatchRegExp(globs) {
Expand Down
42 changes: 42 additions & 0 deletions test/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DataURLsNotSupportedError,
TabUtils,
asQueryCode,
fixUrl,
makeBatchUrl,
makePostUrl,
makeUrl,
Expand Down Expand Up @@ -37,6 +38,47 @@ describe("asQueryCode()", function() {
});
});

describe("fixUrl()", function() {
const testCases = [
{
name: "bilibili",
url: "https://i0.hdslb.com/bfs/album/7cebff5e5f45b17a7aba554bef68b6e84a5f483a.jpg@240w_320h_1e_1c.webp",
expected: "https://i0.hdslb.com/bfs/album/7cebff5e5f45b17a7aba554bef68b6e84a5f483a.jpg",
},
{
name: "discord",
url: "https://media.discordapp.net/attachments/310432830138089472/722011243862556772/omegalbert_2.png?width=400&height=274",
expected: "https://media.discordapp.net/attachments/310432830138089472/722011243862556772/omegalbert_2.png",
},
{
name: "pinterest",
url: "https://i.pinimg.com/736x/73/e8/2d/73e82d272de705bb8fad33e89c0543e5.jpg",
expected: "https://i.pinimg.com/originals/73/e8/2d/73e82d272de705bb8fad33e89c0543e5.jpg",
},
{
name: "fanbox",
url: "https://pixiv.pximg.net/c/1620x580_90_a2_g5/fanbox/public/images/creator/228078/cover/tHi9VtLFvJW4RS1h1DVpttRQ.jpeg",
expected: "https://pixiv.pximg.net/fanbox/public/images/creator/228078/cover/tHi9VtLFvJW4RS1h1DVpttRQ.jpeg",
},
{
name: "booth",
url: "https://booth.pximg.net/c/300x300_a2_g5/14df0a03-2f5f-4292-bb5a-94a3881df4f0/i/2926394/24c0b971-8807-4d40-8089-bdbf34089056_base_resized.jpg",
expected: "https://booth.pximg.net/14df0a03-2f5f-4292-bb5a-94a3881df4f0/i/2926394/24c0b971-8807-4d40-8089-bdbf34089056.jpg",
},
{
name: "fantia",
url: "https://c.fantia.jp/uploads/post/file/709449/main_324b4503-c64b-428b-875c-eaa273861268.png",
expected: "https://c.fantia.jp/uploads/post/file/709449/324b4503-c64b-428b-875c-eaa273861268.png",
},
]

for (let t of testCases) {
it(t.name, function() {
should(fixUrl(t.url)).equal(t.expected);
});
}
});

describe("makeBatchUrl()", function() {
it("", function() {
const url = makeBatchUrl(prefix, pageUrl);
Expand Down

0 comments on commit 04827b4

Please sign in to comment.