Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- #3769: Various OMEMO fixes
- #3791: Fetching pubsub node configuration fails
- #3792: Node reconfiguration attempt uses incorrect field names
- Adding support for protocol handler
- Fix documentation formatting in security.rst
- Add approval banner in chats with requesting contacts or unsaved contacts
- Add mongolian as a language option
Expand Down
2 changes: 1 addition & 1 deletion dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
muc_show_logs_before_join: true,
notify_all_room_messages: ['discuss@conference.conversejs.org'],
fetch_url_headers: true,
whitelisted_plugins: ['converse-debug'],
whitelisted_plugins: ['converse-debug','converse-protocol-handler'],
bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
// view_mode: 'overlayed',
show_controlbox_by_default: true,
Expand Down
8 changes: 7 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
"background_color": "#397491",
"display": "standalone",
"scope": "/",
"theme_color": "#397491"
"theme_color": "#397491",
"protocol_handlers": [
{
"protocol": "xmpp",
"url": "/dev?jid=%s"
}
]
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import "./plugins/rosterview/index.js";
import "./plugins/singleton/index.js";
import "./plugins/dragresize/index.js"; // Allows chat boxes to be resized by dragging them
import "./plugins/fullscreen/index.js";
import "./plugins/protocol-handler/index.js"; // Handle xmpp: links
/* END: Removable components */

_converse.exports.CustomElement = CustomElement;
Expand Down
58 changes: 58 additions & 0 deletions src/plugins/protocol-handler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @description Plugin for handling XMPP protocol links and opening chats.
* @license Mozilla Public License (MPLv2)
*/
import { api, converse } from '@converse/headless';

converse.plugins.add('converse-protocol-handler', {
dependencies: [], // No dependencies needed

initialize () {
let jidFromUrl = null; // Store the JID from the URL for later use
// The first step is to register the protocol handler on app initialization
if ('registerProtocolHandler' in navigator) {
try {
// Defining the URL pattern for the protocol handler so that the browser knows where to redirect
// when an xmpp: link is clicked. The %s will be replaced by the full JID, using /dev as by default it was redirecting to the website.
const handlerUrl = `${window.location.origin}/dev?jid=%s`;
navigator.registerProtocolHandler('xmpp', handlerUrl)
} catch (error) {
console.warn('Failed to register protocol handler:', error);
}
} else {
// If the browser doesn't support it, we can't do much
return;
}

// If the protocol is registered , we parse the JID from the URL on page load
const urlParams = new URLSearchParams(window.location.search);
jidFromUrl = urlParams.get('jid'); // e.g., 'xmpp:user@example.com'

if (jidFromUrl) {
// Sanitize: Remove 'xmpp:' prefix if present
if (jidFromUrl.startsWith('xmpp:')) {
jidFromUrl = jidFromUrl.substring(5);
}
// Clean up the URL
const newUrl = window.location.pathname + window.location.hash;
window.history.replaceState({}, document.title, newUrl);
}
// If already connected, open the chat immediately
if (jidFromUrl) {
api.chats.open(jidFromUrl).then(() => {
const chatbox = api.chatboxes.get(jidFromUrl);
chatbox.show(); // Bring to front
})
}
// Open the chat only after the user logs in (connects)
api.listen.on('connected', () => {
if (jidFromUrl) {
api.chats.open(jidFromUrl).catch((error) => {
console.error('Failed to open chat for JID:', jidFromUrl, error);
});
// Clear the JID after opening to avoid re-opening on reconnect
jidFromUrl = null;
}
});
}
});
Loading