Copy settings

Add buttons to Lightspeed backoffice to copy settings into another shop. Works while you are on the same page, e.g. /

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Copy settings
// @version      1.01
// @description  Add buttons to Lightspeed backoffice to copy settings into another shop. Works while you are on the same page, e.g. /
// @author       Ruben Van Hee
// @changelog    created
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @run-at       document-end
// @match        https://*.webshopapp.com/admin/*
// @match        https://*.shoplightspeed.com/admin/*
// @namespace https://greasyfork.org/users/428438
// ==/UserScript==
(function() {
	'use strict';
    settingscopier();
    function settingscopier(){
// ------------------- set buttons & styles
		$('#content .PageHeader h1').append('<a class="LabelLetterSpacing LabelPadding LineHeightMedium Rounded BorderNone Uppercase Bold VAlignMiddle Ml-half Text-xxs tc Label-default" id="copys">Copy</a> <a class="LabelLetterSpacing LabelPadding LineHeightMedium Rounded BorderNone Uppercase Bold VAlignMiddle Ml-half Text-xxs tc Label-default" id="pastes">Paste</a>');
// ------------------- get vars & selectors
		let shopdomain = location.hostname.split(".")[location.hostname.split(".").length-2]+'.'+location.hostname.split(".")[location.hostname.split(".").length-1];
		let $copybutton = document.getElementById('copys');
		let $pastebutton = document.getElementById('pastes');
		let $form = $('form[id]')[0];
		let $input = $($form).serialize() || 'no input';
		let form_id = 'form#'+$form.id;
		let d = new Date();d.setTime(d.getTime() + (30*1000));
		let ed = d.toUTCString();
// ------------------- copy first shop and set as cookie
		$copybutton.addEventListener('click',function(e){
			/**/console.log($input);
			document.cookie = `shopset=${$input}; domain=${shopdomain}; expires=${ed};`;
			checkmark('copys');
		});
// ------------------- paste function
		$pastebutton.addEventListener('click',function(e){
			function getCookie(coname) {
				coname = coname + "=";
				let decodedCookie = document.cookie;
				let ca = decodedCookie.split(';');
				for(var i = 0; i <ca.length; i++) {
					let c = ca[i];
					while (c.charAt(0) == ' ') {
						c = c.substring(1);
					}
					if (c.indexOf(coname) == 0) {
						return c.substring(coname.length, c.length);
					}
				}
				return c;
			}
			let data = getCookie('shopset'); //get cookie with settings from copied shop
			let arr = data.split('&');
			$.each(arr, function(i,item){
				item.match('authenticity_token')?arr[i]="authenticity_token="+$form.authenticity_token.value:'';
			})
			data = arr.join('&');
			/**/console.log(data);
			jQuery.post(jQuery(form_id).attr('action'), data, function(d){console.log(d);}).fail(function(xhr, status, error){/**/console.log('FAILED', xhr, status, error);});
			setTimeout(1500, location.reload);
		});
// -------------------
		function checkmark(button_id){
			body.append('<style>#copys:after, #pastes:after{content:"yes";}</style>');
		}
	}
// ------------------- second init, because tampermonkey
	document.addEventListener("page:load", function() {
		settingscopier();
	});
})();