Paperless: default currency

Workaround to set the default currency for custom monetary fields in Paperless up to version 2.11.2.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Paperless: default currency
// @namespace   https://denilson.sa.nom.br/
// @author      Denilson Sá Maia
// @version     1.0
// @description Workaround to set the default currency for custom monetary fields in Paperless up to version 2.11.2.
// @icon        https://docs.paperless-ngx.com/assets/favicon.png
// @license     Public Domain
// @match       https://demo.paperless-ngx.com/*
// ==/UserScript==

// HOW TO USE THIS SCRIPT
// ----------------------
//
// 1. Make sure your browser can run user scripts. In doubt, just install the [Violentmonkey extension](https://violentmonkey.github.io/).
// 2. Click on `Install this script` from <https://greasyfork.org/en/scripts/502490-paperless-default-currency>
// 3. Click on the green `+ Edit` button.
// 4. Edit the `@match` line, replace `https://demo.paperless-ngx.com/*` with your Paperless installation. (e.g. `https://127.0.0.1:9493/*` )
// 5. Edit the `DEFAULT_CURRENCY` below to your preferred currency.
//
// See also: <https://github.com/paperless-ngx/paperless-ngx/discussions/6040>

(function () {
	"use strict";

  const DEFAULT_CURRENCY = 'EUR';

	//////////////////////////////////////////////////
	// Convenience functions

	// Returns a new function that will call the callback without arguments
	// after timeout milliseconds of quietness.
	function debounce(callback, timeout = 500) {
		let id = null;
		return function() {
			clearTimeout(id);
			id = setTimeout(callback, timeout);
		};
	}

	const active_mutation_observers = [];

	// Returns a new MutationObserver that observes a specific node.
	// The observer will be immediately active.
	function debouncedMutationObserver(rootNode, callback, timeout = 500) {
		const func = debounce(callback, timeout);
		func();
		const observer = new MutationObserver(func);
		observer.observe(rootNode, {
			subtree: true,
			childList: true,
			attributes: false,
		});
		active_mutation_observers.push(observer);
		return observer;
	}

	// Adds a MutationObserver to each root node matched by the CSS selector.
	function debouncedMutationObserverSelectorAll(rootSelector, callback, timeout = 500) {
		for (const root of document.querySelectorAll(rootSelector)) {
			debouncedMutationObserver(root, callback, timeout);
		}
	}

	function stopAllMutationObservers() {
		for (const mo of active_mutation_observers) {
			mo.disconnect();
		}
		active_mutation_observers.length = 0;
	}

	//////////////////////////////////////////////////

	function main() {
		debouncedMutationObserverSelectorAll('body', function() {
			for (const input of document.querySelectorAll('pngx-input-monetary.ng-untouched.ng-pristine input.form-control.text-muted.ng-untouched.ng-pristine[maxlength="3"]')) {
				if (input.value === 'USD') {
					input.value = DEFAULT_CURRENCY;
					const ev = new InputEvent('input', { data: DEFAULT_CURRENCY });
					input.dispatchEvent(ev);
				}
			}
		});
	}

	main();

})();