Skip to content

Commit

Permalink
rewrite blurnsfw
Browse files Browse the repository at this point in the history
  • Loading branch information
zerebos committed Nov 10, 2018
1 parent 3b331e6 commit 5315eb8
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 91 deletions.
202 changes: 111 additions & 91 deletions Plugins/BlurNSFW/BlurNSFW.plugin.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,117 @@
//META{"name":"BlurNSFW","website":"https://github.com/rauenzi/BetterDiscordAddons/tree/master/Plugins/BlurNSFW","source":"https://github.com/rauenzi/BetterDiscordAddons/blob/master/Plugins/BlurNSFW/BlurNSFW.plugin.js"}*//
//META{"name":"BlurNSFW","displayName":"BlurNSFW","website":"https://github.com/rauenzi/BetterDiscordAddons/tree/master/Plugins/BlurNSFW","source":"https://raw.githubusercontent.com/rauenzi/BetterDiscordAddons/master/Plugins/BlurNSFW/BlurNSFW.plugin.js"}*//

/* global DiscordModules:false, PluginUtilities:false, InternalUtilities:false, BdApi:false */
var BlurNSFW = (() => {
if (!global.ZLibrary && !global.ZLibraryPromise) global.ZLibraryPromise = new Promise((resolve, reject) => {
require("request").get({url: "https://rauenzi.github.io/BDPluginLibrary/release/ZLibrary.js", timeout: 10000}, (err, res, body) => {
if (err || 200 !== res.statusCode) return reject(err || res.statusMessage);
try {const vm = require("vm"), script = new vm.Script(body, {displayErrors: true}); resolve(script.runInThisContext());}
catch(err) {reject(err);}
});
});
const config = {"info":{"name":"BlurNSFW","authors":[{"name":"Zerebos","discord_id":"249746236008169473","github_username":"rauenzi","twitter_username":"ZackRauen"}],"version":"0.2.1","description":"Blurs images in NSFW channels until you hover over it. Support Server: bit.ly/ZeresServer","github":"https://github.com/rauenzi/BetterDiscordAddons/tree/master/Plugins/BlurNSFW","github_raw":"https://raw.githubusercontent.com/rauenzi/BetterDiscordAddons/master/Plugins/BlurNSFW/BlurNSFW.plugin.js"},"changelog":[{"title":"What's New?","items":["Rewrite to new library."]}],"main":"index.js"};
const compilePlugin = ([Plugin, Api]) => {
const plugin = (Plugin, Api) => {
const {Patcher, WebpackModules, DiscordModules, PluginUtilities} = Api;

class BlurNSFW {
getName() { return "BlurNSFW"; }
getShortName() { return "bnsfw"; }
getDescription() { return "Blurs images in NSFW channels until you hover over it. Support Server: bit.ly/ZeresServer"; }
getVersion() { return "0.2.0"; }
getAuthor() { return "Zerebos"; }
const SelectedChannelStore = DiscordModules.SelectedChannelStore;
const ChannelStore = DiscordModules.ChannelStore;
const ReactDOM = DiscordModules.ReactDOM;
const InlineMediaWrapper = WebpackModules.getByProps("ImageReadyStates").default;

constructor() {
this.style = `:root {--blur-nsfw: 10px; --blur-nsfw-time: 200ms;}
img.blur:hover,
video.blur:hover {
transition: var(--blur-nsfw-time) cubic-bezier(.2, .11, 0, 1) !important;
filter: blur(0px) !important;
}
img.blur,
video.blur {
filter: blur(var(--blur-nsfw)) !important;
transition: var(--blur-nsfw-time) cubic-bezier(.2, .11, 0, 1) !important;
}`;
this.cancels = [];
}

load() {}
unload() {}

start() {
let libraryScript = document.getElementById('zeresLibraryScript');
if (!libraryScript || (window.ZeresLibrary && window.ZeresLibrary.isOutdated)) {
if (libraryScript) libraryScript.parentElement.removeChild(libraryScript);
libraryScript = document.createElement("script");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://rauenzi.github.io/BetterDiscordAddons/Plugins/PluginLibrary.js");
libraryScript.setAttribute("id", "zeresLibraryScript");
document.head.appendChild(libraryScript);
}

if (window.ZeresLibrary) this.initialize();
else libraryScript.addEventListener("load", () => { this.initialize(); });
}

initialize() {
this.initialized = true;
PluginUtilities.checkForUpdate(this.getName(), this.getVersion());
BdApi.injectCSS(this.getShortName(), this.style);
return class BlurNSFW extends Plugin {
constructor() {
super();
this.style = `:root {--blur-nsfw: 10px; --blur-nsfw-time: 200ms;}
img.blur:hover,
video.blur:hover {
transition: var(--blur-nsfw-time) cubic-bezier(.2, .11, 0, 1) !important;
filter: blur(0px) !important;
}
img.blur,
video.blur {
filter: blur(var(--blur-nsfw)) !important;
transition: var(--blur-nsfw-time) cubic-bezier(.2, .11, 0, 1) !important;
}`;
}

let SelectedChannelStore = DiscordModules.SelectedChannelStore;
let ChannelStore = DiscordModules.ChannelStore;
let ReactDOM = DiscordModules.ReactDOM;
let InlineMediaWrapper = InternalUtilities.WebpackModules.findByUniqueProperties(['ImageReadyStates']).default;
onStart() {
(function (Api) {
if (window.ZeresPluginLibrary) return; // they already have it
const hasShownAnnouncement = Api.PluginUtilities.loadData(this.getName(), "announcements", {localLibNotice: false}).localLibNotice;
if (hasShownAnnouncement) return;
Api.Modals.showConfirmationModal("Local Library Notice", Api.DiscordModules.React.createElement("span", null, `This version of ${this.getName()} is the final version that will be released using a remotely loaded library. Future versions will require my local library that gets placed in the plugins folder.`, Api.DiscordModules.React.createElement("br"), Api.DiscordModules.React.createElement("br"), "You can download the library now to be prepared, or wait until the next version which will prompt you to download it."), {
confirmText: "Download Now",
cancelText: "Wait",
onConfirm: () => {
require("electron").shell.openExternal("https://betterdiscord.net/ghdl?url=https://raw.githubusercontent.com/rauenzi/BDPluginLibrary/master/release/0PluginLibrary.plugin.js");
}
});
Api.PluginUtilities.saveData(this.getName(), "announcements", {localLibNotice: true});
})(Api);
PluginUtilities.addStyle(this.getName(), this.style);
const blurAccessory = (thisObject) => {
const channel = ChannelStore.getChannel(SelectedChannelStore.getChannelId());
if (!channel.isNSFW()) return;
const element = ReactDOM.findDOMNode(thisObject);
const mediaElement = element.querySelector("img") || element.querySelector("video");
if (!mediaElement) return;

mediaElement.classList.add("blur");

if (mediaElement.tagName !== "VIDEO") return;
mediaElement.addEventListener("play", () => {
if (mediaElement.autoplay) return;
mediaElement.classList.remove("blur");
});
mediaElement.addEventListener("pause", () => {
if (mediaElement.autoplay) return;
mediaElement.classList.add("blur");
});
};

Patcher.after(InlineMediaWrapper.prototype, "componentDidMount", blurAccessory);
Patcher.after(InlineMediaWrapper.prototype, "componentDidUpdate", blurAccessory);
}

onStop() {
Patcher.unpatchAll();
PluginUtilities.removeStyle(this.getName());
}

let blurAccessory = (thisObject) => {
let channel = ChannelStore.getChannel(SelectedChannelStore.getChannelId());
if (!channel.isNSFW()) return;
let element = ReactDOM.findDOMNode(thisObject);
let mediaElement = element.querySelector("img") || element.querySelector("video");
if (!mediaElement) return;

mediaElement.classList.add("blur");

if (mediaElement.tagName !== "VIDEO") return;
mediaElement.addEventListener("play", () => {
if (mediaElement.autoplay) return;
mediaElement.classList.remove("blur");
});
mediaElement.addEventListener("pause", () => {
if (mediaElement.autoplay) return;
mediaElement.classList.add("blur");
});
};

Patcher.after(this.getName(), InlineMediaWrapper.prototype, "componentDidMount", blurAccessory);
Patcher.after(this.getName(), InlineMediaWrapper.prototype, "componentDidUpdate", blurAccessory);
PluginUtilities.showToast(this.getName() + " " + this.getVersion() + " has started.");
}

stop() {
Patcher.unpatchAll(this.getName());
BdApi.clearCSS(this.getShortName());
}

getSettingsPanel() {
var panel = $("<form>").addClass("form").css("width", "100%");
var header = $('<div class="formNotice-2tZsrh margin-bottom-20 padded card-3DrRmC">');
var headerText = $('<div class="default-3bB32Y formText-1L-zZB formNoticeBody-1C0wup whiteText-32USMe modeDefault-389VjU primary-2giqSn">');
headerText.html("To update the blur amount change the css variable <span style='font-family: monospace;'>--blur-nsfw</span> to something like <span style='font-family: monospace;'>20px</span>. <br> You can also change the tranistion time by changing <span style='font-family: monospace;'>--blur-nsfw-time</span> to something like <span style='font-family: monospace;'>500ms</span>");
headerText.css("line-height", "150%");
headerText.appendTo(header);
header.appendTo(panel);
return panel[0];
}
}
getSettingsPanel() {
var panel = $("<form>").addClass("form").css("width", "100%");
var header = $("<div class=\"formNotice-2tZsrh margin-bottom-20 padded card-3DrRmC\">");
var headerText = $("<div class=\"default-3bB32Y formText-1L-zZB formNoticeBody-1C0wup whiteText-32USMe modeDefault-389VjU primary-2giqSn\">");
headerText.html("To update the blur amount change the css variable <span style='font-family: monospace;'>--blur-nsfw</span> to something like <span style='font-family: monospace;'>20px</span>. <br> You can also change the tranistion time by changing <span style='font-family: monospace;'>--blur-nsfw-time</span> to something like <span style='font-family: monospace;'>500ms</span>");
headerText.css("line-height", "150%");
headerText.appendTo(header);
header.appendTo(panel);
return panel[0];
}

};
};
return plugin(Plugin, Api);
};

return !global.ZLibrary ? class {
getName() {return config.info.name.replace(" ", "");} getAuthor() {return config.info.authors.map(a => a.name).join(", ");} getDescription() {return config.info.description;} getVersion() {return config.info.version;} stop() {}
showAlert() {window.BdApi.alert("Loading Error",`Something went wrong trying to load the library for the plugin. You can try using a local copy of the library to fix this.<br /><br /><a href="https://betterdiscord.net/ghdl?url=https://raw.githubusercontent.com/rauenzi/BDPluginLibrary/master/release/0PluginLibrary.plugin.js" target="_blank">Click here to download the library!</a>`);}
async load() {
try {await global.ZLibraryPromise;}
catch(err) {return this.showAlert();}
const vm = require("vm"), plugin = compilePlugin(global.ZLibrary.buildPlugin(config));
try {new vm.Script(plugin, {displayErrors: true});} catch(err) {return bdpluginErrors.push({name: this.getName(), file: this.getName() + ".plugin.js", reason: "Plugin could not be compiled.", error: {message: err.message, stack: err.stack}});}
global[this.getName()] = plugin;
try {new vm.Script(`new global["${this.getName()}"]();`, {displayErrors: true});} catch(err) {return bdpluginErrors.push({name: this.getName(), file: this.getName() + ".plugin.js", reason: "Plugin could not be constructed", error: {message: err.message, stack: err.stack}});}
bdplugins[this.getName()].plugin = new global[this.getName()]();
bdplugins[this.getName()].plugin.load();
}
async start() {
try {await global.ZLibraryPromise;}
catch(err) {return this.showAlert();}
bdplugins[this.getName()].plugin.start();
}
} : compilePlugin(global.ZLibrary.buildPlugin(config));
})();
4 changes: 4 additions & 0 deletions Plugins/BlurNSFW/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# BlurNSFW Changelog

### 0.2.1

- Rewrite to new lib

### 0.2.0

- Rewrite using internals (should be more consistent)
Expand Down

0 comments on commit 5315eb8

Please sign in to comment.