Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name WhatsApp Web+ Beta
- // @namespace de.liltv.apps.web.waplus
- // @version 0.2
- // @description Enhance WhatsApp Web with new options provided by this script
- // @author liltv.media
- // @match *://web.whatsapp.com/*
- // @grant none
- // ==/UserScript==
- // Build: 21.06.2017 17:50
- // ===========================================================================
- // ===========================================================================
- /* Initializing WhatsApp Web+ JS Class and hook to window namespace */
- window.WAWebPlus = {};
- var wawp = window.WAWebPlus;
- /* Initialize class structure */
- wawp.core = {
- version: "0.2",
- buildtime: "21.06.2017 17:50"
- };
- wawp.helper = {
- lang: {},
- log: {},
- init: {
- inject: {}
- },
- gui: {},
- uri: {},
- backup: {}
- };
- wawp.cache = {
- prefs: {},
- qresps: {},
- chats: {},
- msgs: {},
- current: {
- id: 0,
- name: "",
- info: ""
- }
- };
- wawp.defaults = {
- storage: {
- prefs: {
- enabled: true,
- man_chatTitle: true,
- man_wideTextBubbles: true,
- man_chatBackground: false,
- man_chatBackground_img: false,
- css_colorbarTop: true,
- css_colorbarTopColor: "#009668",
- css_imgRoundness: "7%",
- css_drawerHeaderColor: "#00bfa5",
- css_paneHeaderColor: "#eee",
- css_chatBackground_color: "#fffcef",
- css_chatBackground_img: ""
- },
- qresps: {}
- }
- };
- var waz = wawp.core;
- var wah = wawp.helper;
- var wac = wawp.cache;
- var wad = wawp.defaults;
- var wal = wah.lang;
- var wai = wah.init;
- var wag = wah.gui;
- var wau = wah.uri;
- var wab = wah.backup;
- var wao = wah.log;
- var wap = wac.prefs;
- var waq = wac.qresps;
- var way = wac.current;
- var was = wad.storage;
- var wp = window.localStorage;
- var ws = window.sessionStorage;
- var wl = document.location;
- // ===========================================================================
- /** Initializes WAWebPlus
- * @namespace WAWebPlus.core
- * @method init
- */
- waz.init = function (){
- //# If WAWebPlus was not installed before, run setup
- if(wp.getItem("WAWebPlus") !== "true"){
- waz.setup();
- }
- //# Get installed version, compare with this version and if older, make backup and run setup
- let ver = wp.getItem("WAWebPlus:version");
- let comp = wah.compareVersionStr(ver, waz.version);
- if(comp.isOlder === true){
- wab.createCompatBackup();
- waz.setup();
- wag.promptLoadCompatBackup();
- }
- if(comp.isNewer === true){
- wag.showNewerVersionInstalledInfo();
- }
- //# Load preferences, quick-responses
- wai.loadPrefs();
- wai.loadQuickResps();
- //# If WAWebPlus is disabled in Preferences, quit execution
- if(wap.enabled !== "true"){
- return false;
- }
- //# Inject css and custom HTML for GUI
- wai.inject.css_man();
- wai.inject.css_gui();
- wai.inject.html_gui();
- //# Initialize (250ms) interval for checking whether WA has loaded
- wai.intvLoaded = setInterval(wai.checkWALoaded, 250);
- //# Bind WAWebPlus after-loading init to WALoaded event
- window.addEventListener("WALoaded", wai.initAfterLoading);
- //# Bind WAWebPlus closing action handler to before-unload event
- window.addEventListener("beforeunload", waz.closingHandler);
- };
- /** Setup for first-time install or newer version install
- * @namespace WAWebPlus.core
- * @method setup
- */
- waz.setup = function (){
- //# Create indicator and version information in permanent local storage
- wp.setItem("WAWebPlus", "true");
- wp.setItem("WAWebPlus:version", waz.version);
- //# Load preferences from defaults and write to permanent local storage
- for (pref in was.prefs){
- //# Safety check for for-in array loop (https://stackoverflow.com/a/9329476)
- if(was.prefs.hasOwnProperty(pref)){
- //# Set item in permanent local storage
- wp.setItem("WAWebPlus:p:" + pref, was.prefs[pref]);
- }
- }
- //# Load quick responses from defaults and write to permanent local storage
- for (qresp in was.qresps){
- //# Safety check for for-in array loop (https://stackoverflow.com/a/9329476)
- if(was.qresps.hasOwnProperty(qresp)){
- //# Set item in local storage
- wp.setItem("WAWebPlus:q:" + qresp, was.qresps[qresp]);
- }
- }
- };
- /** Core function; will be called at every mouse click or every 12 seconds
- * @namespace WAWebPlus.core
- * @method run
- */
- waz.run = function (){
- };
- // ===========================================================================
- /** Direct user call to disable WAWebPlus
- * @namespace WAWebPlus
- * @method disable
- */
- wawp.disable = function (){
- //# Write disabled property to permanent local storage
- wp.setItem("WAWebPlus:p:enabled", false);
- //# Reload WAWeb to kill timers etc.
- wl.reload();
- };
- /** Direct user call to enable WAWebPlus again
- * @namespace WAWebPlus
- * @method enable
- */
- wawp.enable = function (){
- //# Write disabled property to permanent local storage
- wp.setItem("WAWebPlus:p:enabled", true);
- //# Reload WAWeb to initalize WAWebPlus
- wl.reload();
- };
- // ===========================================================================
- /** Load preferences from permanent local storage to array cache
- * @namespace WAWebPlus.helper.init
- * @method loadPrefs
- */
- wai.loadPrefs = function (){
- //# Loop through default preferences and get the values for those preferences
- for (pref in was.prefs){
- //# Safety check for for-in array loop (https://stackoverflow.com/a/9329476)
- if(was.prefs.hasOwnProperty(pref)){
- //# Check if permanent local storage has that preference stored
- if(wp.getItem("WAWebPlus:p:" + pref) !== null){
- wap[pref] = wp.getItem("WAWebPlus:p:" + pref);
- }
- else{
- //# Preference not stored, use default value instead
- wap[pref] = was.prefs[pref];
- }
- }
- }
- };
- /** Load quick-responses from permanent local storage to array cache
- * @namespace WAWebPlus.helper.init
- * @method loadQuickResps
- */
- wai.loadQuickResps = function (){
- //# Loop through default preferences and get the values for those preferences
- for (qresp in was.qresps){
- //# Safety check for for-in array loop (https://stackoverflow.com/a/9329476)
- if(was.qresps.hasOwnProperty(qresp)){
- //# Check if permanent local storage has that preference stored
- if(wp.getItem("WAWebPlus:p:" + qresp) !== null){
- waq[qresp] = wp.getItem("WAWebPlus:p:" + qresp);
- }
- else{
- //# Preference not stored, use default value instead
- waq[qresp] = was.qresps[qresp];
- }
- }
- }
- };
- /** Event to be fired when WAWeb has finished loading the main interface
- * @namespace WAWebPlus.helper.init
- * @event WALoaded
- */
- wai.WALoaded = new Event("WALoaded");
- /** Test whether main interface of WAWeb has finished loading
- * @namespace WAWebPlus.helper.init
- * @method checkWALoaded
- */
- wai.checkWALoaded = function (){
- //# Test for existence of app-div
- if(!!document.querySelector("div.app")){
- //# If existing, fire "loaded" event
- window.dispatchEvent(wai.WALoaded);
- return true;
- }
- return false;
- };
- /** Function called after WA has finished loading
- * @namespace WAWebPlus.helper.init
- * @method initAfterLoading
- */
- wai.initAfterLoading = function (){
- //# Clear interval for checking whether WAWeb has loaded
- clearInterval(wai.intvLoaded);
- //# Bind WAWebPlus run to mouse-clicks and to a 12 seconds interval
- window.addEventListener("mouseup", waz.run);
- wai.mainInterval = setInterval(waz.run, 12000);
- //# Start first WAWebPlus run
- waz.run();
- };
- // ===========================================================================
- /** Inject css manipulations into page
- * @namespace WAWebPlus.helper.init.inject
- * @method css_man
- */
- wai.inject.css_man = function (){
- //# Create string for CSS manipulations
- let csstr = "";
- //# Image roundness
- csstr += ".avatar, .avatar-image, .avatar-overlay, .preview-image, .metadata-icon, .bubble-audio .preview { border-radius: " + wap.css_imgRoundness + " !important; } ";
- //# Custom color background topbar
- if(wap.css_colorbarTop === "true"){
- csstr += ".app-wrapper::after { background-color: " + wap.css_colorbarTopColor + " !important; } ";
- }
- else{
- csstr += ".app-wrapper::after { display:none !important; } ";
- }
- //# Wide text bubbles
- if(wap.man_wideTextBubbles === "true"){
- csstr += ".message-in, .message-out { max-width: 100% !important; } ";
- }
- //# Chat background
- if(wap.man_chatBackground === "true"){
- if(wap.man_chatBackground_img === "true"){
- csstr += ".pane-chat-msgs.pane-chat-body { background-image: url('" + wap.css_chatBackground_img + "') !important; background-size:cover !important; background-position:fixed !important; background-repeat: none; } ";
- }
- else{
- csstr += ".pane-chat-msgs.pane-chat-body { background-color: " + wap.css_chatBackground_color + " !important; } ";
- }
- }
- //// TODO
- //# Create style-Element, apply style-string as content, append to page
- let cssel = document.createElement("style");
- cssel.innerHTML = csstr;
- cssel.id = "WAWebPlus_css_mod";
- document.body.appendChild(cssel);
- };
- /** Inject css for GUI into page
- * @namespace WAWebPlus.helper.init.inject
- * @method css_gui
- */
- wai.inject.css_gui = function (){
- //// TODO
- };
- /** Inject html for gui into page
- * @namespace WAWebPlus.helper.init.inject
- * @method html_gui
- */
- wai.inject.html_gui = function (){
- //// TODO
- };
- // ===========================================================================
- /** Compare two version strings
- * @namespace WAWebPlus.helper
- * @method compVersionStr
- * @param str left String to test
- * @param str right String to test left against
- * @return obj Object with comparison result
- */
- wah.compareVersionStr = function (left, right){
- // Adapted from https://stackoverflow.com/a/6832706/3144667
- var nft = false;
- //# Check if same
- if(left === right){
- var res = 0;
- }
- //# Split at the dots
- var left_comp = left.split(".");
- var right_comp = right.split(".");
- //# Calculate minimum version string difference
- var len = Math.min(left_comp.length, right_comp.length);
- //# Loop through parts of version string
- for(var i = 0; i < len; i++){
- if(parseInt(left_comp[i]) > parseInt(right_comp[i])){
- var res = 1;
- var nft = true;
- }
- if(parseInt(left_comp[i]) < parseInt(right_comp[i])){
- var res = -1;
- var nft = true;
- }
- }
- //# Check for length of ver string otherwise - longer is newer
- if(!nft){
- if(left_comp.length > right_comp.length){
- var res = 1;
- }
- if(left_comp.length < right_comp.length){
- var res = -1;
- }
- }
- //# Parse result to object
- switch(res){
- case 0:
- var obj = { isOlder: false, isNewer: false, isSame: true, numeric:0 };
- break;
- case 1:
- var obj = { isOlder: false, isNewer: true, isSame: false, numeric:1 };
- break;
- case -1:
- var obj = { isOlder: true, isNewer: false, isSame: false, numeric:-1 };
- break;
- default:
- var obj = { isOlder: false, isNewer: false, isSame: false, numeric:2 };
- break;
- }
- return obj;
- };
- /** Determine if startscreen is opened
- * @namespace WAWebPlus.helper
- * @method startScreenOpened
- * @return bool
- */
- wah.startScreenOpened = function (){
- //# Return state of existence of start screen
- return !!document.querySelector("div.intro.pane-body");
- };
- /** Determine if a chat is opened
- * @namespace WAWebPlus.helper
- * @method chatOpened
- * @return bool
- */
- wah.chatOpened = function (){
- //# Return state of existence of start screen
- return !!document.querySelector("div.pane-chat-tile");
- };
- // ===========================================================================
- /* Finally start everything on load */
- window.addEventListener("load", WAWebPlus.core.init);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement