-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
115 lines (98 loc) · 2.95 KB
/
Copy pathcontent.js
File metadata and controls
115 lines (98 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function getPlatformByUrl(url) {
if (!url || !url.startsWith('http')) {
console.error(`Invalid URL: ${url}`);
return null;
}
try {
const hostname = new URL(url).hostname.toLowerCase();
for (const platform of Object.values(window.PLATFORMS)) {
if (platform.domains.some(domain =>
hostname === domain || hostname.endsWith(`.${domain}`)
)) {
return platform;
}
}
console.error(`No platform found for URL: ${url}`);
return null;
} catch (error) {
console.error(`Get platform from URL ${url}:`, error);
return null;
}
}
function getAnalyzerForPlatformAnalyzer(platformName) {
if (!platformName) {
console.error("Platform name is required");
return null;
}
const platform = window.PLATFORMS[platformName];
if (!platform || !platform.analyzer) {
console.error(`No analyzer found for platform: ${platformName}`);
return null;
}
return new platform.analyzer();
}
function initializeExtension() {
const platform = getPlatformByUrl(window.location.href);
if (!platform) {
console.log("Unsupported platform or URL");
return;
}
if (!platform.analyzer) {
console.error(`No analyzer found for platform: ${platformName}`);
return null;
}
const analyzer = new platform.analyzer();
console.log(`initializeExtension for platform: ${platform.name}`);
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => analyzer.insertDownloadButton());
} else {
analyzer.insertDownloadButton();
}
}
// Start extension initialization
initializeExtension();
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const { platformName, action } = message;
switch (action) {
case "extractVideoPageInfo":
extractVideoPageInfo(platformName, sendResponse);
break;
default:
console.warn("Unknown message type:", action);
sendResponse({ success: false, error: "Unknown message type" });
}
return true;
});
function extractVideoPageInfo(platformName, sendResponse) {
try {
console.log(`extractVideoPageInfo for platform: ${platformName}`);
const analyzer = getAnalyzerForPlatformAnalyzer(platformName);
if (!analyzer) {
console.error(`No analyzer found for platform: ${platformName}`);
sendResponse({
success: false,
error: `Unsupported platform: ${platformName}`,
});
return;
}
const videoPageInfo = analyzer.extractVideoPageInfo();
console.log(`extractVideoPageInfo for platform: ${platformName} videoPageInfo:`, videoPageInfo);
if (videoPageInfo !== null) {
sendResponse({
success: true,
data: videoPageInfo,
});
} else {
sendResponse({
success: false,
error: "No video info found",
});
}
} catch (error) {
console.error(`Error in extractVideoPageInfo: ${error.message}`);
sendResponse({
success: false,
error: error.message,
});
}
}