Skip to content

Instantly share code, notes, and snippets.

@Cologler
Last active September 25, 2017 09:13
Show Gist options
  • Save Cologler/6acbbf2fee962796e6873dc546f11278 to your computer and use it in GitHub Desktop.
Save Cologler/6acbbf2fee962796e6873dc546f11278 to your computer and use it in GitHub Desktop.
#user_script_lib
// ==UserScript==
// @name QS
// @namespace https://github.com/cologler/
// @version 0.1.6.1
// @description a helper for QuickSettings.
// @author cologler (skyoflw@gmail.com)
// @grant none
// @license MIT
// @require https://cdn.jsdelivr.net/quicksettings/latest/quicksettings.min.js
// ==/UserScript==
// this lib was hosting on https://gist.github.com/Cologler/6acbbf2fee962796e6873dc546f11278.
// you can just require: https://gist.github.com/Cologler/6acbbf2fee962796e6873dc546f11278/raw/qs.js
// this lib was hosting on https://greasyfork.org/scripts/33447
// you can just require: https://greasyfork.org/scripts/33447-qs/code/QS.js
// just let type script work.
(function() { function require(){}; require("greasemonkey"); })();
let QS = (function() {
'use strict';
const KEY_MODULE = '62474676-69b2-4df3-a123-59192d28cdc0';
const win = (() => {
try {
return unsafeWindow || window;
} catch (error) {
if (error instanceof ReferenceError) {
return window;
}
throw error;
}
})();
win[KEY_MODULE] = win[KEY_MODULE] || (() => {
const EVENT_PANEL_CREATING = 'fa25696c-decc-45b3-b0f9-c57fa77f3a3a';
const EVENT_PANEL_CREATED = 'dc2726e8-6077-4734-8b99-34847204f321';
let defaultPanel = null;
class Panel {
constructor(...creationOptions) {
this._qs = null;
Object.defineProperties(this, {
creationOptions: {
value: creationOptions
}
});
}
get isOpened() {
return this._qs !== null;
}
switch() {
if (this._qs) {
this.close();
} else {
this.open();
}
}
open() {
if (this._qs === null) {
this._qs = QuickSettings.create(...this.creationOptions);
document.dispatchEvent(new CustomEvent(EVENT_PANEL_CREATING, {
detail: {
source: this,
qs: this._qs
}
}));
document.dispatchEvent(new CustomEvent(EVENT_PANEL_CREATED, {
detail: {
source: this,
qs: this._qs
}
}));
}
}
close() {
if (this._qs !== null) {
this._qs.destroy();
this._qs = null;
}
}
static get default() {
return defaultPanel;
}
static set default(val) {
defaultPanel = val;
}
}
function useDefault() {
if (!Panel.default) {
Panel.default = new Panel(50, 50, '<Options>', null);
registerAsMenuCommand(Panel.default);
document.addEventListener(EVENT_PANEL_CREATING, e => {
if (e.detail.source === Panel.default) {
e.detail.qs._panel.style.position = 'fixed';
}
});
}
return Panel.default;
}
function registerAsMenuCommand(qs, title) {
title = title || '<Options>';
const handler = GM_registerMenuCommand(title, () => {
qs.switch();
});
return {
dispose: () => {
GM_unregisterMenuCommand(handler);
}
};
}
return {
EVENT_PANEL_CREATING,
EVENT_PANEL_CREATED,
Panel,
registerAsMenuCommand,
useDefault
};
})();
return win[KEY_MODULE];
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment