Copy settings

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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();
	});
})();