Skip to content

Instantly share code, notes, and snippets.

@Gesugao-san
Last active March 25, 2022 09:54
Show Gist options
  • Save Gesugao-san/e8a27dcc93db1ee4610c95b17cd962da to your computer and use it in GitHub Desktop.
Save Gesugao-san/e8a27dcc93db1ee4610c95b17cd962da to your computer and use it in GitHub Desktop.
Open Discord guild widget (markbooklet)

Requirements

  • Google Chrome with latest version (mine is: 94).

How to use

Deploying

  1. Go to https://caiorss.github.io/bookmarklet-maker/;
  2. Paste code from "Open_Discord_guild_widget.js";
  3. Click "Generate Bookmarklet" button;
  4. Ctrl+Shift+B to show bookmarks bar
  5. Create bookmark in Google Chrome (with any name, example: "Discord guild widget");
  6. In "URL" field insert code from https://caiorss.github.io/bookmarklet-maker/.

Using

  1. ToDo...

Tools and inspiration


License

MIT 2022 Gesugao-san


javascript: (function() {
'use strict';
var settings = {"target_hrefs": ["://discord.com/channels"], "exclude_str": ["@me"]};
var data = {"detectedLinks": []};
function open_widget(id) {
if (!id) id = prompt("Discord guild ID?") || false;
if (id === false) {
console.error("Error: Discord guild ID is not provided!");
return 1;
}
if (isNaN(parseInt(id))) {
console.error("Error: Discord guild ID should be integer!");
return 1;
}
if ((id < 0) || (id > 9223372036854775807)) {
console.error("Error: Discord guild ID should be between 0 and 9223372036854775807!");
return 1;
}
window.open("https://discord.com/widget?id="+id+"&theme=dark");
return 0;
}
/* if (settings['target_hrefs'].filter(href => href.startsWith(window.location.href)).length === 0) { */
if (!window.location.href.includes(settings['target_hrefs'][0])) {
open_widget();
return 1;
}
console.debug(settings['target_hrefs'].filter(href => href.startsWith(window.location.href)));
console.debug("Seems to logged in...");
if (window.location.href.includes(settings['exclude_str'][0])) {
console.warn("But this for guilds only.");
open_widget();
return 1;
}
var hrefs = document.querySelectorAll('[href]');
for (var i = 0; i < hrefs.length; ++i) {
var item = hrefs[i].getAttribute("href");
if (item === null) continue;
if (!item.includes("channels") || item.includes(settings['exclude_str'][0])) continue;
data['detectedLinks'].push(item);
}
if (data['detectedLinks'].length === 0) {
console.warn("Error: No channels!");
open_widget(href_splitted[1]);
return 1;
}
console.debug("detectedLinks:", data['detectedLinks']);
var href_splitted = data['detectedLinks'][Math.round(Math.random() * data['detectedLinks'].length)].split('/');
if (href_splitted[0] == "") href_splitted.shift();
if (href_splitted.length < 3) {
console.error("Error: Low href lenght!");
return 1;
}
console.debug("href_splitted:", href_splitted);
open_widget(window.location.href.split('/')[4]);
return 0;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment