Skip to content

Commit b178641

Browse files
committed
unshorten url
1 parent 691f75d commit b178641

File tree

8 files changed

+155
-9
lines changed

8 files changed

+155
-9
lines changed
579 Bytes
Binary file not shown.

background-scripts/background.js

Whitespace-only changes.

manifest.json

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,47 @@
1313
"default_popup": "./popup/popup.html",
1414
"default_icon": "./assets/icon32.png"
1515
},
16-
"permissions": ["tabs", "scripting", "storage", "cookies"],
17-
"host_permissions": ["<all_urls>"],
16+
"permissions": [
17+
"tabs",
18+
"scripting",
19+
"storage",
20+
"cookies",
21+
"declarativeNetRequest",
22+
"declarativeNetRequestFeedback",
23+
"declarativeNetRequestWithHostAccess"
24+
],
25+
"host_permissions": [
26+
"<all_urls>"
27+
],
1828
"options_page": "./pages/options/options.html",
1929
"content_scripts": [
2030
{
21-
"matches": ["<all_urls>"],
22-
"js": ["content-scripts/document_start.js"],
31+
"matches": [
32+
"<all_urls>"
33+
],
34+
"js": [
35+
"content-scripts/document_start.js"
36+
],
2337
"run_at": "document_start"
2438
}
2539
],
26-
"web_accessible_resources": [{
27-
"resources": ["content-scripts/*.js"],
28-
"matches": ["<all_urls>"]
29-
}]
30-
}
40+
"web_accessible_resources": [
41+
{
42+
"resources": [
43+
"content-scripts/*.js"
44+
],
45+
"matches": [
46+
"<all_urls>"
47+
]
48+
}
49+
],
50+
"declarative_net_request": {
51+
"rule_resources": [
52+
{
53+
"id": "ruleset_1",
54+
"enabled": true,
55+
"path": "net-request-rules/rules.json"
56+
}
57+
]
58+
}
59+
}

net-request-rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/

net-request-rules/rules.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
{
3+
"id": 1,
4+
"priority": 1,
5+
"action": {
6+
"type": "modifyHeaders",
7+
"requestHeaders": [
8+
{
9+
"header": "Referer",
10+
"operation": "set",
11+
"value": "https://unshorten.it/"
12+
},
13+
{
14+
"header": "origin",
15+
"operation": "set",
16+
"value": "https://unshorten.it/"
17+
}
18+
]
19+
},
20+
"condition": {
21+
"domain": "extension://*",
22+
"urlFilter": "https://unshorten.it/main/get_long_url",
23+
"resourceTypes": [
24+
"xmlhttprequest"
25+
]
26+
}
27+
}
28+
]

popup/tabs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ const tabs = [
160160
{
161161
...CATEGORY.unlock,
162162
scripts: [
163+
s.unshorten,
163164
s.showHiddenFields,
164165
s.viewCookies,
165166
s.removeCookies,

scripts/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import fb_getTokenLocmai from "./fb_getTokenLocmai.js";
106106
import fb_checkToken from "./fb_checkToken.js";
107107
import fb_getTokenCampaigns from "./fb_getTokenCampaigns.js";
108108
import test_script from "./test_script.js";
109+
import unshorten from "./unshorten.js";
109110

110111
// inject badges
111112
const allScripts = {
@@ -220,6 +221,7 @@ const allScripts = {
220221
fb_getTokenLocmai: addBadge(fb_getTokenLocmai, BADGES.beta),
221222
fb_checkToken: addBadge(fb_checkToken, BADGES.new),
222223
fb_getTokenCampaigns: addBadge(fb_getTokenCampaigns, BADGES.new),
224+
unshorten: addBadge(unshorten, BADGES.hot),
223225
test_script: test_script,
224226
};
225227

scripts/unshorten.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
export default {
2+
icon: "https://lh3.googleusercontent.com/xwarvPJ490JDNBNlB4_nVOE3KEs-A6xI07luVNP--iQ7kipstjiSHf-S1rofE-ji9E0clqa_vkivURh42UOA3uXsmHw=w128-h128-e365-rj-sc0x00ffffff",
3+
name: {
4+
en: "Unshorten link",
5+
vi: "Giải mã link rút gọn",
6+
},
7+
description: {
8+
en: "Get origin URL of shortened url",
9+
vi: "Lấy link gốc của link rút gọn",
10+
},
11+
runInExtensionContext: true,
12+
13+
func: async function () {
14+
// Để script này hoạt động được, cần thêm rule modify header referer
15+
// Chi tiết xem trong file rules.json
16+
17+
// https://unshorten.it/
18+
19+
async function getToken() {
20+
let res = await fetch("https://unshorten.it/");
21+
let text = await res.text();
22+
let token = /name='csrfmiddlewaretoken' value='(.*)'/.exec(text)?.[1];
23+
return token;
24+
}
25+
26+
async function getLongUrl(shortURL, token) {
27+
let formData = new FormData();
28+
formData.append("short-url", shortURL);
29+
formData.append("csrfmiddlewaretoken", token);
30+
31+
let res = await fetch("https://unshorten.it/main/get_long_url", {
32+
method: "POST",
33+
body: formData,
34+
});
35+
let json = await res.json();
36+
if (json?.success) {
37+
return json.long_url;
38+
} else {
39+
alert(json.message);
40+
return null;
41+
}
42+
}
43+
44+
let shortenURL = prompt("Nhập URL đã rút gọn: ");
45+
if (shortenURL) {
46+
// faster way: open unshorten.it page
47+
// return window.open(
48+
// "http://unshorten.it/extensionloading.php?shortURL=" +
49+
// shortenURL +
50+
// "&source=chromeextension"
51+
// );
52+
53+
try {
54+
let token = await getToken();
55+
console.log(token);
56+
let long_url = await getLongUrl(shortenURL, token);
57+
58+
if (long_url) prompt("Link gốc của " + shortenURL, long_url);
59+
else alert("Không tìm thấy link gốc");
60+
} catch (e) {
61+
alert("Lỗi: " + e);
62+
}
63+
}
64+
},
65+
};
66+
67+
// modify header referer in manifest v3
68+
// https://stackoverflow.com/a/72739149/11898496
69+
70+
// manifest v2 only, background script only
71+
// https://stackoverflow.com/a/31003808/11898496
72+
// https://stackoverflow.com/a/56141157/11898496
73+
// chrome.webRequest.onBeforeSendHeaders.addListener(
74+
// function (details) {
75+
// let headers = details.requestHeaders;
76+
// headers.push({
77+
// name: "Referer",
78+
// value: "https://unshorten.it/",
79+
// });
80+
// console.log(headers);
81+
// return { requestHeaders: headers };
82+
// },
83+
// { urls: ["https://unshorten.it/main/get_long_url"] },
84+
// ["blocking", "requestHeaders", "extraHeaders"]
85+
// );

0 commit comments

Comments
 (0)