Created
July 26, 2019 03:08
-
-
Save mohemohe/8e6be4b2ab2541237f4b7a6ede98251a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // ==UserScript== | |
| // @name Mastodon X Files | |
| // @namespace mizle.net | |
| // @description :thinking_face: | |
| // @author Eai <eai@mizle.net> | |
| // @license MIT | |
| // @version 1.2.2 | |
| // @include https://*/web/* | |
| // @grant GM.xmlHttpRequest | |
| // ==/UserScript== | |
| /*global player*/ | |
| "use strict"; | |
| window.addEventListener( | |
| "load", | |
| function() { | |
| class Xfiles { | |
| constructor() { | |
| const xfiles = "https://cldup.com/cnkhRDR-D3.mp3"; | |
| this.cached = null; | |
| GM.xmlHttpRequest({ | |
| method: "GET", | |
| url: xfiles, | |
| responseType: "arraybuffer", | |
| onload: async (response) => { | |
| this.cached = response.response; | |
| } | |
| }); | |
| this.mutationObserver = new MutationObserver(mutations => | |
| this.onUpdate(mutations) | |
| ); | |
| } | |
| start() { | |
| this.mutationObserver.observe( | |
| document | |
| .querySelector(".fa-home.column-header__icon") // Home Column Icon | |
| .closest("div.column") // Home Column | |
| .querySelector("div.item-list") || document.querySelector(".columns-area__panels__main"), | |
| { | |
| childList: true, | |
| } | |
| ); | |
| } | |
| onUpdate(mutations) { | |
| mutations.forEach(mutation => { | |
| if (mutation.addedNodes.length != 0) { | |
| const node = mutation.addedNodes["0"]; | |
| var text = node.textContent; | |
| // 全角英数変換 | |
| text = text.replace(/[A-Za-z0-9]/g, function(s) { | |
| return String.fromCharCode(s.charCodeAt(0) - 65248); | |
| }); | |
| // スペース削除 | |
| text = text.replace(/\s+/g, ""); | |
| if (/x[- ]?files/gim.test(text)) { | |
| console.log("X-Files Theme Detected", node); | |
| this.play(); | |
| } | |
| } | |
| }); | |
| } | |
| async play() { | |
| const context = new AudioContext(); | |
| var volume = 0.5; | |
| const settings = | |
| JSON.parse( | |
| localStorage.getItem("mizle.net_x-files theme") | |
| ) || {}; | |
| if ("volume" in settings) { | |
| const gain = context.createGain(); | |
| gain.connect(context.destination); | |
| gain.gain.value = settings.volume; | |
| } | |
| if (this.cached !== null) { | |
| const audioBuffer = await context.decodeAudioData(this.cached); | |
| const source = context.createBufferSource(); | |
| source.buffer = audioBuffer; | |
| source.connect(context.destination); | |
| source.start(); | |
| } | |
| } | |
| } | |
| new Xfiles().start(); | |
| }, | |
| false | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment