-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
108 lines (97 loc) · 3.68 KB
/
background.js
File metadata and controls
108 lines (97 loc) · 3.68 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
// Default settings object
let settings = {
spotify: 'off',
youtube: 'off',
ytmusic: 'off',
twitch: 'off'
};
// Load settings from storage
function loadSettings() {
chrome.storage.sync.get(['appLinkSettings'], (data) => {
if (data.appLinkSettings) {
settings = data.appLinkSettings;
}
});
}
// Initial settings load
loadSettings();
// Listen for settings changes
chrome.storage.onChanged.addListener((changes) => {
if (changes.appLinkSettings) {
settings = changes.appLinkSettings.newValue || settings;
}
});
// Intercept web navigation
chrome.webNavigation.onBeforeNavigate.addListener((details) => {
if (details.frameId !== 0) return;
const url = details.url;
if (!url.startsWith('http')) return;
let targetUri = null;
let service = null;
// Check for YouTube Music
if (url.startsWith('http://music.youtube.com/') || url.startsWith('https://music.youtube.com/')) {
service = 'ytmusic';
if (settings.ytmusic !== 'off') {
const rest = url.replace(/^https?:\/\/music\.youtube\.com\//, '');
targetUri = 'youtubemusic://music.youtube.com/' + rest;
}
}
// Check for YouTube
else if (url.match(/^https?:\/\/(www\.)?youtube\.com\//)) {
service = 'youtube';
if (settings.youtube !== 'off') {
const rest = url.replace(/^https?:\/\/(www\.)?youtube\.com\//, '');
targetUri = 'youtube://youtube.com/' + rest;
}
} else if (url.match(/^https?:\/\/youtu\.be\//)) {
service = 'youtube';
if (settings.youtube !== 'off') {
const rest = url.replace(/^https?:\/\/youtu\.be\//, '');
targetUri = 'youtube://youtu.be/' + rest;
}
}
// Check for Spotify
else if (url.includes('spotify.com') || url.includes('spotify.com')) {
service = 'spotify';
if (settings.spotify !== 'off') {
const match = url.match(/(track|artist|album|playlist)\/(.*)/);
if (match) {
targetUri = 'spotify://' + match[1] + '/' + match[2];
}
}
}
// Check for Twitch
else if (url.match(/^https?:\/\/(www\.|m\.|clips\.)?twitch\.tv/)) {
service = 'twitch';
if (settings.twitch !== 'off') {
const urlObj = new URL(url);
const path = urlObj.pathname;
// Map specific Twitch routes to deep links
if (path.startsWith('/videos/')) {
const vid = path.split('/')[2];
targetUri = 'twitch://video/' + vid;
} else if (path.startsWith('/directory/game/')) {
const game = path.split('/')[3];
targetUri = 'twitch://game/' + game;
} else if (urlObj.hostname === 'clips.twitch.tv' || path.includes('/clip/')) {
targetUri = 'twitch://' + urlObj.hostname + path;
} else {
const matchStream = path.match(/^\/([a-zA-Z0-9_]+)$/);
if (matchStream && !['directory', 'downloads', 'jobs', 'p'].includes(matchStream[1])) {
targetUri = 'twitch://stream/' + matchStream[1];
} else {
targetUri = 'twitch://' + urlObj.hostname + path + urlObj.search;
}
}
}
}
// Redirect if target URI is generated
if (targetUri && service && settings[service] !== 'off') {
let finalUrl = targetUri;
// Encode and route via LiveContainer if enabled
if (settings[service] === 'livecontainer') {
finalUrl = 'livecontainer://open-web-page?url=' + btoa(targetUri);
}
chrome.tabs.update(details.tabId, { url: finalUrl });
}
});