Skip to content

Commit

Permalink
Add magnet protocol handling in browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
6c65726f79 committed Aug 25, 2021
1 parent 3f28020 commit 1511a06
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ The file can contain the following key/value pairs:
| useBits | `boolean` | Display speed in bit/s |
| expandMenu | `boolean` | Expand side menu on large screen |
| ipFlags | `boolean` | Display peer flag |
| openMagnetLinks | `boolean` | Open magnet links with Transmissionic |
| refreshInterval | `number` | Refresh interval in seconds |
| timeout | `number` | Connection timeout in seconds |

Expand Down
2 changes: 1 addition & 1 deletion src/services/FileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const FileHandler = {
document.body.addEventListener("drop",(e) => this.handleFilesDrop(e), false);

// Read hash from URL
const hash = window.location.hash.substring(1)
const hash = decodeURIComponent(window.location.hash.substring(1));
hash.startsWith("url:") ? this.readURL(hash.substring(4)) : this.readHashOrMagnet(hash);
},
async inputFile(): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion src/services/UserSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const defaultSettings: Record<string,any> = {
selectedServer:0,
useBits:true,
expandMenu:true,
ipFlags:false
ipFlags:false,
openMagnetLinks:false
}

export const UserSettings = {
Expand Down
10 changes: 10 additions & 0 deletions src/services/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,15 @@ export const Utils = {
reader.readAsDataURL(blob)
}))
document.querySelector("link[title=ATI]")?.setAttribute("href",data);
},
registerMagnetLinkProtocol(): void {
if(UserSettings.state.openMagnetLinks){
if(navigator.registerProtocolHandler!==null){
navigator.registerProtocolHandler("magnet", window.location.href + "#%s", "Transmissionic Magnet Handler" );
}
}
else {
navigator.unregisterProtocolHandler("magnet", window.location.href + "#%s");
}
}
}
1 change: 1 addition & 0 deletions src/views/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export default defineComponent({
async beforeCreate() {
await UserSettings.loadSettings();
this.privateState.selectedServer = UserSettings.state.selectedServer;
Utils.registerMagnetLinkProtocol();
if(UserSettings.state.language=="default"){
await LocaleController.setLanguage(UserSettings.getLanguage());
}
Expand Down
24 changes: 19 additions & 5 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@
</ion-item>
</ion-list>

<ion-list v-if="protocolHandlerAvailable">
<ion-list-header>
<ion-label>
{{ Locale.protocols }}
</ion-label>
</ion-list-header>

<ion-item>
<ion-label>{{ Locale.openMagnetLinks }}</ion-label>
<ion-toggle v-model="sharedState.openMagnetLinks"></ion-toggle>
</ion-item>
</ion-list>

<ion-list v-if="bookmarkletEnabled">
<ion-list-header>
<ion-label>
Expand Down Expand Up @@ -177,7 +190,7 @@ const bookmarkletFunction = (href: string) => {
}
});
}
found ? window.open(href+"#"+selection) : alert("No torrent or magnet link found.");
found ? window.open(href+"#"+encodeURIComponent(selection)) : alert("No torrent or magnet link found.");
}
export default defineComponent({
Expand Down Expand Up @@ -210,6 +223,9 @@ export default defineComponent({
computed: {
bookmarkletEnabled(): boolean {
return !isPlatform("electron") && !isPlatform("capacitor")
},
protocolHandlerAvailable(): boolean {
return !isPlatform("electron") && !isPlatform("capacitor") && navigator.registerProtocolHandler!==null
}
},
setup() {
Expand All @@ -233,14 +249,12 @@ export default defineComponent({
methods: {
saveSettings () {
UserSettings.saveSettings();
this.savedToast();
Utils.responseToast("success");
Utils.registerMagnetLinkProtocol();
},
modalClose () {
modalController.dismiss();
},
async savedToast() {
Utils.responseToast("success")
},
async resetSettings(){
const alert = await alertController
.create({
Expand Down

0 comments on commit 1511a06

Please sign in to comment.