Skip to content

Instantly share code, notes, and snippets.

@langheran
Last active May 10, 2022 18:52
Show Gist options
  • Save langheran/4bf1a4cf911596b2ba7f4233cf895d8d to your computer and use it in GitHub Desktop.
Save langheran/4bf1a4cf911596b2ba7f4233cf895d8d to your computer and use it in GitHub Desktop.
google cloud console MonitorElementSound.user.js
// ==UserScript==
// @name MonitorElementSound
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @include /^https://[^\/]*(console|pipelines|aws|github|gitlab|notebook|jupyter)[^\/]*\.com/.*/
// @icon https://www.google.com/s2/favicons?sz=64&domain=qwiklabs.com
// @grant GM_xmlhttpRequest
// @connect lasonotheque.org
// @run-at document-end
// ==/UserScript==
class AudioState {
constructor() {
// The AudioContext cannot be instanciated before action of the user.
// This is a small lazy trick.
var context;
this.getContext = function() {
if (typeof context === "undefined")
{
window.AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
}
return context;
};
this.ticTacSrc;
}
};
function playFromBuffer( state, buffer ) {
var src = state.getContext().createBufferSource();
src.buffer = buffer;
src.connect( state.getContext().destination );
src.start( 0 );
return src;
}
function playFromBufferLoop( state, buffer ) {
var src = state.getContext().createBufferSource()
var gainNode = state.getContext().createGain();
src.buffer = buffer;
gainNode.gain.value = 0.2;
src.connect(gainNode);
gainNode.connect( state.getContext().destination );
src.start( 0 );
src.loop = true;
return src;
}
function playTicTac( state ) {
console.timeEnd();
console.time();
GM_xmlhttpRequest( {
method: "GET",
url: 'https://lasonotheque.org/UPLOAD/mp3/2137.mp3',
responseType: 'arraybuffer',
onload: function( response ) {
try {
state.getContext().decodeAudioData(
response.response,
buff => { state.ticTacSrc = playFromBufferLoop( state, buff ); },
console.error );
}
catch( e ) {
console.error( e );
}
}
});
}
function playExplosion( state ) {
GM_xmlhttpRequest( {
method: "GET",
url: 'https://lasonotheque.org/UPLOAD/mp3/1023.mp3',
responseType: 'arraybuffer',
onload: function( response ) {
try {
state.getContext().decodeAudioData(
response.response,
buff => { state.ticTacSrc = playFromBuffer( state, buff ); },
console.error );
}
catch( e ) {
console.error( e );
}
}
});
console.timeEnd();
}
(function() {
'use strict';
// alert('aaa');
var shiftDown = false;
var ctrlDown = false;
var altDown = false;
function doc_keyUp(e) {
switch (e.keyCode) {
case 16:
shiftDown = false;
break;
case 17:
ctrlDown = false;
break;
case 18:
altDown = false;
break;
case 27:
state.ticTacSrc.disconnect();
mergeDoneObserver.disconnect();
break;
default:
break;
}
}
function doc_keyDown(e) {
switch (e.keyCode) {
case 16:
shiftDown = true;
break;
case 17:
ctrlDown = true;
break;
case 18:
altDown = true;
break;
default:
break;
}
}
function onClick(e){
monitorElement = e.srcElement;
oldHref = document.location.href;
if (!(ctrlDown && shiftDown))
{
return;
}
else{
shiftDown = false;
ctrlDown = false;
altDown = false;
e.preventDefault();
monitorText = prompt("Expresión regular a monitorear en "+monitorElement.tagName+"?", "");
if (monitorText === null) {
return;
}
if (monitorText==""){
monitorText=false;
}
// alert('Monitoring changes in DOM element.');
}
state.getContext().resume();
//monitorElement = document.querySelectorAll( ":hover" )[0];
mergeDoneObserver.observe(
monitorElement,
{
childList: true,
subtree: true,
attributes: true,
attributeOldValue: true,
characterData: true,
attributeOldValue: true,
characterDataOldValue: true
}
);
mergeDoneObserver.observe(monitorElement.parentNode, { subtree: false, childList: true });
try{
state.ticTacSrc.disconnect();
}catch{
}
playTicTac( state );
}
unsafeWindow.onclick = onClick;
document.addEventListener('keyup', doc_keyUp, false);
document.addEventListener('keydown', doc_keyDown, false);
// var player = document.createElement('audio');
// player.src = 'https://sounds-mp3.com/mp3/0002597.mp3';
// player.preload = 'auto';
// player.play();
// Your code here...\
let monitorElement;
let oldHref;
let state = new AudioState();
let explosionDownloaded;
let monitorText;
let mergeDoneObserver = new MutationObserver( function ( mutations, me ) {
// mergeDone nodes list will not be empty when the merge message appears.
// var mergeDone = mutations.flatMap( m => [...m.addedNodes] )
// .filter( i => i.nodeType <= 2 )
// .filter( i => i.id === "partial-pull-merging" );
let changed = false;
mutations.forEach((mutation) => {
const { target } = mutation;
if (mutation.type === 'attributes'){
if (mutation.attributeName === 'class' || mutation.attributeName === 'style') {
changed = true;
}
else
{
if (mutation.attributeName.match(/data-.*/)){
changed = true;
}
}
}
else
{
// alert(mutation.target.data);
if (monitorText){
var re = new RegExp(monitorText, "gi");
if (mutation.target.data.match(re)){
changed = true;
}
}else{
changed = true;
}
}
});
if (oldHref != document.location.href) {
oldHref = document.location.href;
changed=true;
}
if ( changed ) { //mergeDone.length > 0
me.disconnect();
// Do when explosion download is ready.
state.ticTacSrc.disconnect();
playExplosion( state );
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment