-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
452 additions
and
184 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import browser from "webextension-polyfill"; | ||
import isChromium from "../../common/ts/isChromium"; | ||
import { | ||
clearProxySettings, | ||
updateProxySettings, | ||
} from "../../common/ts/proxySettings"; | ||
import store from "../../store"; | ||
|
||
export default function checkForOpenedTwitchTabs() { | ||
if (store.readyState !== "complete") | ||
return store.addEventListener("load", checkForOpenedTwitchTabs); | ||
|
||
browser.tabs | ||
.query({ url: ["https://www.twitch.tv/*", "https://m.twitch.tv/*"] }) | ||
.then(tabs => { | ||
if (tabs.length === 0) { | ||
if (isChromium) clearProxySettings(); | ||
return; | ||
} | ||
console.log( | ||
`🔍 Found ${tabs.length} opened Twitch tabs: ${tabs | ||
.map(tab => tab.id) | ||
.join(", ")}` | ||
); | ||
if (isChromium) { | ||
updateProxySettings(); | ||
} | ||
store.state.openedTwitchTabs = tabs.map(tab => tab.id); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Tabs } from "webextension-polyfill"; | ||
import getHostFromUrl from "../../common/ts/getHostFromUrl"; | ||
import isChromium from "../../common/ts/isChromium"; | ||
import { updateProxySettings } from "../../common/ts/proxySettings"; | ||
import { twitchTvHostRegex } from "../../common/ts/regexes"; | ||
import store from "../../store"; | ||
|
||
export default function onTabCreated(tab: Tabs.Tab): void { | ||
if (!tab.url) return; | ||
const host = getHostFromUrl(tab.url); | ||
if (twitchTvHostRegex.test(host)) { | ||
console.log(`➕ Opened Twitch tab: ${tab.id}`); | ||
if (isChromium && store.state.openedTwitchTabs.length === 0) { | ||
updateProxySettings(); | ||
} | ||
store.state.openedTwitchTabs.push(tab.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import isChromium from "../../common/ts/isChromium"; | ||
import { clearProxySettings } from "../../common/ts/proxySettings"; | ||
import store from "../../store"; | ||
|
||
export default function onTabRemoved(tabId: number): void { | ||
const index = store.state.openedTwitchTabs.indexOf(tabId); | ||
if (index !== -1) { | ||
console.log(`➖ Closed Twitch tab: ${tabId}`); | ||
store.state.openedTwitchTabs.splice(index, 1); | ||
if (isChromium && store.state.openedTwitchTabs.length === 0) { | ||
clearProxySettings(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Tabs } from "webextension-polyfill"; | ||
import getHostFromUrl from "../../common/ts/getHostFromUrl"; | ||
import isChromium from "../../common/ts/isChromium"; | ||
import { | ||
clearProxySettings, | ||
updateProxySettings, | ||
} from "../../common/ts/proxySettings"; | ||
import { twitchTvHostRegex } from "../../common/ts/regexes"; | ||
import store from "../../store"; | ||
|
||
export default function onTabUpdated( | ||
tabId: number, | ||
changeInfo: Tabs.OnUpdatedChangeInfoType, | ||
tab: Tabs.Tab | ||
): void { | ||
// Also check for `changeInfo.status === "complete"` because the `url` property | ||
// is not always accurate when navigating to a new page. | ||
if (!(changeInfo.url || changeInfo.status === "complete")) return; | ||
|
||
const url = changeInfo.url || tab.url; | ||
const host = getHostFromUrl(url); | ||
const isTwitchTab = twitchTvHostRegex.test(host); | ||
const wasTwitchTab = store.state.openedTwitchTabs.includes(tabId); | ||
|
||
if (isTwitchTab && !wasTwitchTab) { | ||
console.log(`➕ Opened Twitch tab: ${tabId}`); | ||
if (isChromium && store.state.openedTwitchTabs.length === 0) { | ||
updateProxySettings(); | ||
} | ||
store.state.openedTwitchTabs.push(tabId); | ||
} | ||
if (!isTwitchTab && wasTwitchTab) { | ||
const index = store.state.openedTwitchTabs.indexOf(tabId); | ||
if (index !== -1) { | ||
console.log(`➖ Closed Twitch tab: ${tabId}`); | ||
store.state.openedTwitchTabs.splice(index, 1); | ||
if (isChromium && store.state.openedTwitchTabs.length === 0) { | ||
clearProxySettings(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Read a file from the user's computer. | ||
* @param accept | ||
* @returns | ||
*/ | ||
export async function readFile(accept = "text/plain;charset=utf-8") { | ||
return new Promise<string>((resolve, reject) => { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.accept = accept; | ||
input.addEventListener("change", async e => { | ||
const input = e.target as HTMLInputElement; | ||
const file = input.files?.[0]; | ||
if (!file) return reject("No file selected"); | ||
const data = await file.text(); | ||
return resolve(data); | ||
}); | ||
input.click(); | ||
}); | ||
} | ||
|
||
/** | ||
* Save a file to the user's computer. | ||
* @param filename | ||
* @param content | ||
* @param type | ||
*/ | ||
export function saveFile( | ||
filename: string, | ||
content: string, | ||
type = "text/plain;charset=utf-8" | ||
) { | ||
const a = document.createElement("a"); | ||
a.setAttribute("href", `data:${type},` + encodeURIComponent(content)); | ||
a.setAttribute("download", filename); | ||
a.click(); | ||
} |
Oops, something went wrong.