Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tab Scroll
- // https://forum.vivaldi.net/topic/27856/tab-scroll
- // Clicking on an active tab scrolls page to top, clicking it again returns to previous scroll position.
- // Credits to tam710562 from Vivaldi Forum for coming up with the sessionStorage solution, which made this possible.
- {
- function tabScrollExit(tab) {
- tab.removeEventListener('mousemove', tabScrollExit);
- tab.removeEventListener('click', tabScrollTrigger);
- }
- function tabScrollTrigger(tab) {
- chrome.tabs.executeScript({code: tabScrollScript});
- tabScrollExit(tab);
- }
- function tabScroll(e, tab) {
- if (tab.parentNode.classList.contains('active') && e.which === 1 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
- tab.addEventListener('mousemove', tabScrollExit(tab));
- tab.addEventListener('click', tabScrollTrigger(tab));
- }
- }
- const tabScrollScript = '!' + function () {
- var offset = window.pageYOffset;
- if (offset > 0) {
- window.sessionStorage.setItem('tabOffset',offset);
- window.scrollTo(0,0);
- }
- else {
- window.scrollTo(0,window.sessionStorage.getItem('tabOffset')||0);
- }
- } + '();';
- var appendChild = Element.prototype.appendChild;
- Element.prototype.appendChild = function () {
- if (arguments[0].tagName === 'DIV' && arguments[0].classList.contains('tab-header')) {
- setTimeout(function() {
- const trigger = (event) => tabScroll(event, arguments[0]);
- arguments[0].addEventListener('mousedown', trigger);
- }.bind(this, arguments[0]));
- }
- return appendChild.apply(this, arguments);
- }
- }
- /*
- * Restore Methods for chrome.tabs
- * Written by Tam710562
- */
- window.gnoh = Object.assign(window.gnoh || {}, {
- tabs: {
- getAllInWindow: function () {
- let windowId;
- let callback;
- Array.from(arguments).forEach(function (argument) {
- switch (typeof argument) {
- case 'number':
- windowId = argument;
- break;
- default:
- callback = argument;
- break;
- }
- });
- chrome.tabs.query({
- windowId: windowId || vivaldiWindowId
- }, function (tabs) {
- callback(tabs);
- });
- },
- getSelected: function () {
- let windowId;
- let callback;
- Array.from(arguments).forEach(function (argument) {
- switch (typeof argument) {
- case 'number':
- windowId = argument;
- break;
- default:
- callback = argument;
- break;
- }
- });
- chrome.tabs.query({
- active: true,
- windowId: windowId || vivaldiWindowId
- }, function (tabs) {
- const tab = tabs[0];
- if (tab) {
- callback(tab);
- }
- });
- },
- executeScript: function () {
- let tabId;
- let details;
- let callback;
- Array.from(arguments).forEach(function (argument) {
- switch (typeof argument) {
- case 'number':
- tabId = argument;
- break;
- case 'object':
- details = argument;
- break;
- default:
- callback = argument;
- break;
- }
- });
- if (tabId) {
- gnoh.webPageView.callMethod(tabId, 'executeScript', [details, callback]);
- } else {
- gnoh.webPageView.callMethod('executeScript', [details, callback]);
- }
- },
- insertCSS: function () {
- let tabId;
- let details;
- let callback;
- Array.from(arguments).forEach(function (argument) {
- switch (typeof argument) {
- case 'number':
- tabId = argument;
- break;
- case 'object':
- details = argument;
- break;
- default:
- callback = argument;
- break;
- }
- });
- if (tabId) {
- gnoh.webPageView.callMethod(tabId, 'insertCSS', [details, callback]);
- } else {
- gnoh.webPageView.callMethod('insertCSS', [details, callback]);
- }
- }
- },
- webPageView: {
- getSelected(callback) {
- gnoh.tabs.getSelected(function (tab) {
- callback(this.get(tab.id));
- }.bind(this));
- },
- get: function (tabId) {
- return document.getElementById(tabId);
- },
- callMethod: function () {
- let tabId;
- let methodName;
- let args;
- Array.from(arguments).forEach(function (argument) {
- switch (typeof argument) {
- case 'number':
- tabId = argument;
- break;
- case 'string':
- methodName = argument;
- break;
- default:
- args = argument;
- break;
- }
- });
- if (tabId) {
- const wpw = this.get(tabId);
- wpw[methodName].apply(wpw, args);
- } else {
- this.getSelected(function (wpw) {
- wpw[methodName].apply(wpw, args);
- });
- }
- }
- }
- });
- if (!chrome.tabs.getAllInWindow) {
- chrome.tabs.getAllInWindow = gnoh.tabs.getAllInWindow;
- }
- if (!chrome.tabs.getSelected) {
- chrome.tabs.getSelected = gnoh.tabs.getSelected;
- }
- if (!chrome.tabs.executeScript) {
- chrome.tabs.executeScript = gnoh.tabs.executeScript;
- }
- if (!chrome.tabs.insertCSS) {
- chrome.tabs.insertCSS = gnoh.tabs.insertCSS;
- }
- /*
- * Choose Search engine in Address bar
- * Written by Tam710562
- */
- window.gnoh = Object.assign(window.gnoh || {}, {
- generateUUID: function (ids) {
- let d = Date.now() + performance.now();
- let r;
- const id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
- r = (d + Math.random() * 16) % 16 | 0;
- d = Math.floor(d / 16);
- return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
- });
- if (Array.isArray(ids) && ids.includes(id)) {
- return this.generateUUID(ids);
- }
- return id;
- },
- addStyle: function (css, id, isNotMin) {
- this.styles = this.styles || {};
- if (Array.isArray(css)) {
- css = css.join(isNotMin === true ? '\n' : '');
- }
- id = id || this.generateUUID(Object.keys(this.styles));
- this.styles[id] = this.createElement('style', {
- html: css || '',
- 'data-id': id
- }, document.head);
- return this.styles[id];
- },
- encode: {
- regex: function (str) {
- return !str ? str : str.replace(/[-/\\^$*+?.()|[]{}]/g, '\\$&');
- }
- },
- createElement: function (tagName, attribute, parent, inner, options) {
- if (typeof tagName === 'undefined') {
- return;
- }
- if (typeof options === 'undefined') {
- options = {};
- }
- if (typeof options.isPrepend === 'undefined') {
- options.isPrepend = false;
- }
- const el = document.createElement(tagName);
- if (!!attribute && typeof attribute === 'object') {
- for (const key in attribute) {
- if (key === 'text') {
- el.textContent = attribute[key];
- } else if (key === 'html') {
- el.innerHTML = attribute[key];
- } else if (key === 'style' && typeof attribute[key] === 'object') {
- for (const css in attribute.style) {
- el.style[css] = attribute.style[css];
- }
- } else if (key === 'events' && typeof attribute[key] === 'object') {
- for (const event in attribute.events) {
- if (typeof attribute.events[event] === 'function') {
- el.addEventListener(event, attribute.events[event]);
- }
- }
- } else if (typeof el[key] !== 'undefined') {
- el[key] = attribute[key];
- } else {
- if (typeof attribute[key] === 'object') {
- attribute[key] = JSON.stringify(attribute[key]);
- }
- el.setAttribute(key, attribute[key]);
- }
- }
- }
- if (!!inner) {
- if (!Array.isArray(inner)) {
- inner = [inner];
- }
- for (let i = 0; i < inner.length; i++) {
- if (inner[i].nodeName) {
- el.append(inner[i]);
- } else {
- el.append(this.createElementFromHTML(inner[i]));
- }
- }
- }
- if (typeof parent === 'string') {
- parent = document.querySelector(parent);
- }
- if (!!parent) {
- if (options.isPrepend) {
- parent.prepend(el);
- } else {
- parent.append(el);
- }
- }
- return el;
- },
- createElementFromHTML: function (html) {
- return this.createElement('template', {
- html: (html || '').trim()
- }).content;
- },
- override: function (obj, functionName, callback, conditon, runbefore) {
- this._overrides = this._overrides || {};
- let subKey = '';
- try {
- if (obj.ownerDocument === document) {
- this._overrides._elements = this._overrides._elements || [];
- const element = this._overrides._elements.find(function (item) {
- return item.element === obj;
- });
- let id;
- if (element) {
- id = element.id;
- } else {
- id = this.generateUUID(this._overrides._elements.map(function (item) {
- return item.id;
- }));
- this._overrides._elements.push({
- element: obj,
- id: id
- });
- }
- subKey = '_' + id;
- }
- } catch (e) { }
- const key = functionName + '_' + obj.constructor.name + subKey;
- if (!this._overrides[key]) {
- this._overrides[key] = [];
- obj[functionName] = (function (_super) {
- return function () {
- let result;
- let conditon = true;
- for (let i = 0; i < gnoh._overrides[key].length; i++) {
- conditon = conditon && (typeof gnoh._overrides[key][i].conditon !== 'function' && gnoh._overrides[key][i].conditon !== false || typeof gnoh._overrides[key][i].conditon === 'function' && !!gnoh._overrides[key][i].conditon.apply(this, arguments));
- if (conditon === false) {
- continue;
- }
- if (gnoh._overrides[key][i].runbefore === true) {
- gnoh._overrides[key][i].callback.apply(this, arguments);
- }
- }
- if (conditon) {
- result = _super.apply(this, arguments);
- }
- for (let i = 0; i < gnoh._overrides[key].length; i++) {
- if (gnoh._overrides[key][i].runbefore !== true) {
- gnoh._overrides[key][i].callback.apply(this, arguments);
- }
- }
- return result;
- };
- })(obj[functionName]);
- }
- this._overrides[key].push({
- callback: callback,
- conditon: conditon,
- runbefore: runbefore
- });
- return key;
- },
- getReactPropsKey: function (element) {
- if (!this.reactPropsKey) {
- if (!element) {
- element = document.getElementById('browser');
- } else if (typeof element === 'string') {
- element = document.querySelector(element);
- }
- if (!element || element.ownerDocument !== document) {
- return;
- }
- this.reactPropsKey = Object.keys(element).find(function (key) {
- return key.startsWith('__reactProps');
- });
- }
- return this.reactPropsKey;
- }
- });
- (function () {
- 'use strict';
- gnoh.addStyle([
- '.UrlBar-AddressField .OmniDropdown .search-engines-in-address-bar { position: sticky; top: 0; right: 0; left: 0; background: var(--colorBg); height: 32px; box-shadow: 0px -1px var(--colorBorder) inset; z-index: 1; }',
- '.UrlBar-AddressField .OmniDropdown .search-engines-in-address-bar button { background: transparent; border: 0; width: 32px; height: 32px; border-radius: 0; display: inline-flex; align-items: center; justify-content: center; border: 1px solid transparent; }',
- '.UrlBar-AddressField .OmniDropdown .search-engines-in-address-bar button:hover { background-color: var(--colorFgAlpha); }',
- '.UrlBar-AddressField .OmniDropdown .search-engines-in-address-bar button.active { background: var(--colorBgIntense); border-left-color: var(--colorBorder); border-right-color: var(--colorBorder); border-top-color: var(--colorBorder); }',
- '.UrlBar-AddressField .OmniDropdown .search-engines-in-address-bar button.disabled { pointer-events: none; }',
- '.UrlBar-AddressField .OmniDropdown .search-engines-in-address-bar button:first-child { border-left-color: transparent; }'
- ], 'search-engines-in-address-bar');
- const settings = {
- oneClick: true,
- searchEngines: {
- default: undefined,
- defaultPrivate: undefined,
- engines: {}
- },
- };
- const pattern = {
- searchEngines: undefined
- };
- let searchEngineButtons;
- let reactPropsKey;
- function createPatternSearchEngines(searchEngineCollection) {
- settings.searchEngines = {
- default: undefined,
- defaultPrivate: undefined,
- engines: {}
- };
- pattern.searchEngines = undefined;
- const engines = searchEngineCollection.engines.filter(e => e.removed !== true);
- if (engines.length > 0) {
- const regKeywords = [];
- engines.forEach(function (engine) {
- settings.searchEngines.engines[engine.keyword] = engine;
- regKeywords.push(gnoh.encode.regex(engine.keyword));
- });
- pattern.searchEngines = new RegExp('^(' + regKeywords.join('|') + ')\\s(.*)', 'i');
- }
- }
- chrome.storage.local.get({
- 'SEARCH_ENGINE_COLLECTION': {
- engines: []
- }
- }, function (res) {
- createPatternSearchEngines(res.SEARCH_ENGINE_COLLECTION);
- });
- chrome.storage.local.onChanged.addListener(function (changes, namespace) {
- if (changes.SEARCH_ENGINE_COLLECTION) {
- createPatternSearchEngines(changes.SEARCH_ENGINE_COLLECTION.newValue);
- }
- });
- function createSearchEnginesInAddressBar(omniDropdown) {
- const searchEnginesInAddressBar = gnoh.createElement('div', {
- class: 'search-engines-in-address-bar'
- }, omniDropdown, null, {
- isPrepend: true
- });
- const addressfieldEl = document.querySelector('input[type="text"].url.vivaldi-addressfield');
- searchEngineButtons = [];
- Object.values(settings.searchEngines.engines).forEach(function (engine) {
- const searchEngineButton = gnoh.createElement('button', {
- class: 'search-engine-button',
- title: engine.keyword + ' : ' + engine.name,
- events: {
- mousedown: function (event) {
- event.preventDefault();
- event.stopPropagation();
- if (!addressfieldEl) {
- return;
- }
- const match = addressfieldEl.value.match(pattern.searchEngines);
- let value = '';
- if (match) {
- if (match[1] === engine.keyword && !settings.oneClick) {
- return;
- }
- value = engine.keyword + ' ' + match[2];
- } else {
- value = engine.keyword + ' ' + addressfieldEl.value;
- }
- if (settings.oneClick) {
- addressfieldEl[reactPropsKey].onChange({ currentTarget: { value: value } });
- addressfieldEl[reactPropsKey].onKeyDown(new KeyboardEvent('keydown', { key: 'Enter', metaKey: true }));
- } else {
- addressfieldEl[reactPropsKey].onChange({ currentTarget: { value: value } });
- setActiveSearchEngineButton(engine.keyword);
- }
- }
- }
- }, searchEnginesInAddressBar);
- const searchEngineIcon = gnoh.createElement('img', {
- class: 'search-engine-icon',
- src: engine.faviconUrl,
- width: 16,
- height: 16
- }, searchEngineButton);
- searchEngineButtons.push({
- keyword: engine.keyword,
- element: searchEngineButton
- });
- });
- const removeSearchEngineButton = gnoh.createElement('button', {
- class: 'remove-search-engine-button',
- events: {
- mousedown: function (event) {
- event.preventDefault();
- event.stopPropagation();
- if (!addressfieldEl) {
- return;
- }
- const match = addressfieldEl.value.match(pattern.searchEngines);
- let value = '';
- if (match) {
- value = match[2];
- addressfieldEl[reactPropsKey].onChange({ currentTarget: { value: value } });
- setActiveSearchEngineButton('');
- }
- }
- }
- }, searchEnginesInAddressBar, '<svg style="width:16px; height:16px" viewBox="0 0 24 24"><path fill="currentColor" d="M12,2C17.53,2 22,6.47 22,12C22,17.53 17.53,22 12,22C6.47,22 2,17.53 2,12C2,6.47 6.47,2 12,2M15.59,7L12,10.59L8.41,7L7,8.41L10.59,12L7,15.59L8.41,17L12,13.41L15.59,17L17,15.59L13.41,12L17,8.41L15.59,7Z"/></svg>');
- searchEngineButtons.push({
- keyword: '',
- element: removeSearchEngineButton
- });
- const match = addressfieldEl.value.match(pattern.searchEngines);
- setActiveSearchEngineButton(match ? match[1] : '');
- if (!addressfieldEl.dataset.searchEnginesInAddressBar) {
- addressfieldEl.dataset.searchEnginesInAddressBar = '';
- addressfieldEl.addEventListener('input', function (e) {
- const match = e.target.value.match(pattern.searchEngines);
- setActiveSearchEngineButton(match ? match[1] : '');
- });
- }
- }
- function setActiveSearchEngineButton(keyword) {
- searchEngineButtons.forEach(function (seb) {
- if (seb.keyword === keyword) {
- seb.element.classList.add('active');
- if (!settings.oneClick) {
- seb.element.classList.add('disabled');
- }
- } else {
- seb.element.classList.remove('active');
- if (!settings.oneClick) {
- seb.element.classList.remove('disabled');
- }
- }
- });
- }
- gnoh.override(HTMLDivElement.prototype, 'appendChild', function (element) {
- reactPropsKey = gnoh.getReactPropsKey(this);
- if (this[reactPropsKey] && this[reactPropsKey].className === 'observer' && element[reactPropsKey] && element[reactPropsKey].className.indexOf('OmniDropdown') > -1) {
- createSearchEnginesInAddressBar(element)
- }
- });
- })();
- // Backup Search Engines
- // https://forum.vivaldi.net/topic/35443/backup-search-engines
- // Adds functionality to backup and restore search engines in vivaldi://settings/search.
- {
- function _msgSearch(pnt) {
- clearTimeout(_msgTimeout);
- if (pnt === 'backup') {
- _infoSearch.innerText = 'Search engines backup copied to clipboard.';
- }
- else if (pnt === 'restore') {
- _infoSearch.innerText = 'Search engines restored.';
- }
- else {
- _infoSearch.innerText = 'Search engines code error.'
- }
- _msgTimeout = setTimeout(function() {
- _infoSearch.innerText = '';
- }, 5000)
- }
- function _restoreSearch() {
- event.preventDefault();
- event.stopPropagation();
- if (_eventSearch === 'paste') {
- var clipboardData = event.clipboardData || window.clipboardData;
- var engineCode = clipboardData.getData('text');
- }
- else {
- var engineCode = event.dataTransfer.getData('text');
- }
- try {
- var engines = JSON.parse(engineCode);
- }
- catch(err) {
- _msgSearch('error');
- return;
- }
- if ('engines' in engines && 'default' in engines && 'defaultPrivate' in engines) {
- chrome.storage.local.set({'SEARCH_ENGINE_COLLECTION': engines}, function() {
- _msgSearch('restore');
- })
- }
- else {
- _msgSearch('error');
- }
- }
- function _backupSearch() {
- chrome.storage.local.get({'SEARCH_ENGINE_COLLECTION': ''}, function(back) {
- const engines = back.SEARCH_ENGINE_COLLECTION;
- const engineCode = JSON.stringify(engines);
- navigator.clipboard.writeText(engineCode);
- _msgSearch('backup');
- })
- }
- function searchEngines() {
- const styleCheck = document.getElementById('searchEngines');
- if (!styleCheck) {
- const style = document.createElement('style');
- style.type = 'text/css';
- style.id = 'searchEngines';
- style.innerHTML = '#backupSearch, #restoreSearch {margin-left: 6px;}#restoreSearch{width: 130px;margin-top: 6px;}#restoreSearch::-webkit-input-placeholder {opacity: 1;color: var(--colorHighlightBg);text-align: center;}#msgConfirm{margin-left: 12px}';
- document.getElementsByTagName('head')[0].appendChild(style);
- }
- const modCheck = document.getElementById('backupSearch');
- if (!modCheck) {
- const place = document.querySelector('.setting-section > div > .setting-group.unlimited > .setting-single');
- const backupBtn = document.createElement('input');
- backupBtn.setAttribute('type', 'button');
- backupBtn.setAttribute('value', 'Backup');
- backupBtn.id = 'backupSearch';
- place.insertBefore(backupBtn, place.lastChild);
- const restoreInput = document.createElement('input');
- restoreInput.setAttribute('type', 'text');
- restoreInput.setAttribute('placeholder', 'Restore Backup');
- restoreInput.id = 'restoreSearch';
- place.insertBefore(restoreInput, place.lastChild);
- _infoSearch = document.createElement('span');
- _infoSearch.id = 'msgConfirm';
- place.insertBefore(_infoSearch, place.lastChild);
- document.getElementById('backupSearch').addEventListener('click', _backupSearch);
- const restoreSearch = document.getElementById('restoreSearch');
- restoreSearch.addEventListener('paste', function() {
- _eventSearch = 'paste';
- _restoreSearch(event);
- })
- restoreSearch.addEventListener('drop', function() {
- _eventSearch = 'drop';
- _restoreSearch(event);
- })
- _msgTimeout = {};
- }
- }
- const settingsUrl = 'chrome-extension://mpognobbkildjkofajifpdfhcoklimli/components/settings/settings.html?path=';
- chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
- if (changeInfo.url === `${settingsUrl}search`) {
- setTimeout(searchEngines, 100);
- }
- })
- }
- /*
- * Markdown Editor for Notes
- * Written by Tam710562 and sjudenim
- * GNU General Public License v3.0
- *
- * Adds a markdwon toolbar to the notes editor
- */
- (function () {
- const buttons = [
- {
- name: 'Bold',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 384 512"><path d="M304.793 243.891c33.639-18.537 53.657-54.16 53.657-95.693 0-48.236-26.25-87.626-68.626-104.179C265.138 34.01 240.849 32 209.661 32H24c-8.837 0-16 7.163-16 16v33.049c0 8.837 7.163 16 16 16h33.113v318.53H24c-8.837 0-16 7.163-16 16V464c0 8.837 7.163 16 16 16h195.69c24.203 0 44.834-1.289 66.866-7.584C337.52 457.193 376 410.647 376 350.014c0-52.168-26.573-91.684-71.207-106.123zM142.217 100.809h67.444c16.294 0 27.536 2.019 37.525 6.717 15.828 8.479 24.906 26.502 24.906 49.446 0 35.029-20.32 56.79-53.029 56.79h-76.846V100.809zm112.642 305.475c-10.14 4.056-22.677 4.907-31.409 4.907h-81.233V281.943h84.367c39.645 0 63.057 25.38 63.057 63.057.001 28.425-13.66 52.483-34.782 61.284z"></path></svg>',
- markdown: '**',
- wrap: true,
- textDefault: 'bold text',
- },
- {
- name: 'Italic',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 320 512"><path d="M204.758 416h-33.849l62.092-320h40.725a16 16 0 0 0 15.704-12.937l6.242-32C297.599 41.184 290.034 32 279.968 32H120.235a16 16 0 0 0-15.704 12.937l-6.242 32C96.362 86.816 103.927 96 113.993 96h33.846l-62.09 320H46.278a16 16 0 0 0-15.704 12.935l-6.245 32C22.402 470.815 29.967 480 40.034 480h158.479a16 16 0 0 0 15.704-12.935l6.245-32c1.927-9.88-5.638-19.065-15.704-19.065z"></path></svg>',
- markdown: '*',
- wrap: true,
- textDefault: 'italicized text',
- },
- {
- name: 'Strikethrough',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 512 512"><path d="M496 288H16c-8.837 0-16-7.163-16-16v-32c0-8.837 7.163-16 16-16h480c8.837 0 16 7.163 16 16v32c0 8.837-7.163 16-16 16zm-214.666 16c27.258 12.937 46.524 28.683 46.524 56.243 0 33.108-28.977 53.676-75.621 53.676-32.325 0-76.874-12.08-76.874-44.271V368c0-8.837-7.164-16-16-16H113.75c-8.836 0-16 7.163-16 16v19.204c0 66.845 77.717 101.82 154.487 101.82 88.578 0 162.013-45.438 162.013-134.424 0-19.815-3.618-36.417-10.143-50.6H281.334zm-30.952-96c-32.422-13.505-56.836-28.946-56.836-59.683 0-33.92 30.901-47.406 64.962-47.406 42.647 0 64.962 16.593 64.962 32.985V136c0 8.837 7.164 16 16 16h45.613c8.836 0 16-7.163 16-16v-30.318c0-52.438-71.725-79.875-142.575-79.875-85.203 0-150.726 40.972-150.726 125.646 0 22.71 4.665 41.176 12.777 56.547h129.823z"></path></svg>',
- markdown: '~~',
- wrap: true,
- textDefault: 'strikethrough text',
- },
- {
- name: 'Underline',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 16 16"><path d="M.5.5v2h1.25v6c0 2.758 2.805 5 6.25 5s6.25-2.242 6.25-5v-6h1.25v-2h-5v2h1.25v6c0 1.656-1.682 3.002-3.75 3.002S4.25 10.156 4.25 8.5v-6H5.5v-2h-5zM.5 14v2h15v-2H.5z"></path></svg>',
- markdown: '++',
- wrap: true,
- textDefault: 'underline text',
- },
- {
- name: 'Highlight',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 544 512"><path d="M0 479.98L99.92 512l35.45-35.45-67.04-67.04L0 479.98zm124.61-240.01a36.592 36.592 0 0 0-10.79 38.1l13.05 42.83-50.93 50.94 96.23 96.23 50.86-50.86 42.74 13.08c13.73 4.2 28.65-.01 38.15-10.78l35.55-41.64-173.34-173.34-41.52 35.44zm403.31-160.7l-63.2-63.2c-20.49-20.49-53.38-21.52-75.12-2.35L190.55 183.68l169.77 169.78L530.27 154.4c19.18-21.74 18.15-54.63-2.35-75.13z"></path></svg>',
- markdown: '==',
- wrap: true,
- textDefault: 'highlight text',
- },
- {
- name: 'Heading',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 512 512"><path d="M496 80V48c0-8.837-7.163-16-16-16H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.621v128H154.379V96H192c8.837 0 16-7.163 16-16V48c0-8.837-7.163-16-16-16H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.275v320H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.621V288H357.62v128H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.275V96H480c8.837 0 16-7.163 16-16z"></path></svg>',
- markdown: '# ',
- newline: true,
- textDefault: 'heading text',
- },
- {
- name: 'Heading 2',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="10" viewBox="0 0 512 512"><path d="M496 80V48c0-8.837-7.163-16-16-16H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.621v128H154.379V96H192c8.837 0 16-7.163 16-16V48c0-8.837-7.163-16-16-16H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.275v320H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.621V288H357.62v128H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.275V96H480c8.837 0 16-7.163 16-16z"></path></svg>',
- markdown: '## ',
- newline: true,
- textDefault: 'heading 2 text',
- },
- {
- name: 'Heading 3',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="11" height="7" viewBox="0 0 512 512"><path d="M496 80V48c0-8.837-7.163-16-16-16H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.621v128H154.379V96H192c8.837 0 16-7.163 16-16V48c0-8.837-7.163-16-16-16H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.275v320H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.621V288H357.62v128H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.275V96H480c8.837 0 16-7.163 16-16z"></path></svg>',
- markdown: '### ',
- newline: true,
- textDefault: 'heading 3 text',
- },
- {
- name: 'Blockquote',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 24 24"><path d="M14.017 21v-7.391c0-5.704 3.731-9.57 8.983-10.609l.995 2.151c-2.432.917-3.995 3.638-3.995 5.849h4v10h-9.983zm-14.017 0v-7.391c0-5.704 3.748-9.57 9-10.609l.996 2.151c-2.433.917-3.996 3.638-3.996 5.849h3.983v10h-9.983z"></path></svg>',
- markdown: '> ',
- newline: true,
- textDefault: 'blockquote',
- },
- {
- name: 'Ordered List',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" version="1.0" height="16" width="16"><path d="M5.97 3l-.314.344S4.9 4 4.406 4v2c.68 0 1.15-.276 1.594-.53V10h2V3H5.97zM11 6v2h17V6H11zm-4.5 6C5.117 12 4 13.117 4 14.5v.5h2v-.5c0-.217.283-.5.5-.5.217 0 .5.283.5.5l-.03.03-.064.064-2.593 2.5-.313.28V19h5v-2H7.28l.876-.875.125-.094-.03-.03c.502-.41.75-1.02.75-1.5C9 13.117 7.883 12 6.5 12zm4.5 3v2h17v-2H11zm-7 6v2h1.375l-.25.406-.125.22V25h1.5c.217 0 .5.283.5.5 0 .217-.283.5-.5.5H4v2h2.5C7.883 28 9 26.883 9 25.5c0-1.005-.678-1.696-1.53-2.094l.405-.687.125-.25V21H4zm7 3v2h17v-2H11z"></path></svg>',
- markdown: '1. ',
- newline: true,
- textDefault: 'item',
- },
- {
- name: 'Unordered List',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" version="1.1" height="16" width="16"><path d="M 6 5 C 4.894531 5 4 5.894531 4 7 C 4 8.105469 4.894531 9 6 9 C 7.105469 9 8 8.105469 8 7 C 8 5.894531 7.105469 5 6 5 Z M 11 6 L 11 8 L 28 8 L 28 6 Z M 6 14 C 4.894531 14 4 14.894531 4 16 C 4 17.105469 4.894531 18 6 18 C 7.105469 18 8 17.105469 8 16 C 8 14.894531 7.105469 14 6 14 Z M 11 15 L 11 17 L 28 17 L 28 15 Z M 6 23 C 4.894531 23 4 23.894531 4 25 C 4 26.105469 4.894531 27 6 27 C 7.105469 27 8 26.105469 8 25 C 8 23.894531 7.105469 23 6 23 Z M 11 24 L 11 26 L 28 26 L 28 24 Z "></path></svg>',
- markdown: '- ',
- newline: true,
- textDefault: 'item',
- },
- {
- name: 'Code',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"><path d="M24 10.935v2.131l-8 3.947v-2.23l5.64-2.783-5.64-2.79v-2.223l8 3.948zm-16 3.848l-5.64-2.783 5.64-2.79v-2.223l-8 3.948v2.131l8 3.947v-2.23zm7.047-10.783h-2.078l-4.011 16h2.073l4.016-16z"></path></svg>',
- markdown: '`',
- wrap: true,
- textDefault: 'code',
- },
- {
- name: 'Link',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12" viewBox="0 0 24 24"><path d="M13.723 18.654l-3.61 3.609c-2.316 2.315-6.063 2.315-8.378 0-1.12-1.118-1.735-2.606-1.735-4.188 0-1.582.615-3.07 1.734-4.189l4.866-4.865c2.355-2.355 6.114-2.262 8.377 0 .453.453.81.973 1.089 1.527l-1.593 1.592c-.18-.613-.5-1.189-.964-1.652-1.448-1.448-3.93-1.51-5.439-.001l-.001.002-4.867 4.865c-1.5 1.499-1.5 3.941 0 5.44 1.517 1.517 3.958 1.488 5.442 0l2.425-2.424c.993.284 1.791.335 2.654.284zm.161-16.918l-3.574 3.576c.847-.05 1.655 0 2.653.283l2.393-2.389c1.498-1.502 3.94-1.5 5.44-.001 1.517 1.518 1.486 3.959 0 5.442l-4.831 4.831-.003.002c-1.438 1.437-3.886 1.552-5.439-.002-.473-.474-.785-1.042-.956-1.643l-.084.068-1.517 1.515c.28.556.635 1.075 1.088 1.528 2.245 2.245 6.004 2.374 8.378 0l4.832-4.831c2.314-2.316 2.316-6.062-.001-8.377-2.317-2.321-6.067-2.313-8.379-.002z"></path></svg>',
- markdown: ['[', '](https://www.example.com)'],
- textDefault: 'title',
- },
- {
- name: 'Image',
- icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="15" viewBox="0 0 512 512"><path d="M464 448H48c-26.51 0-48-21.49-48-48V112c0-26.51 21.49-48 48-48h416c26.51 0 48 21.49 48 48v288c0 26.51-21.49 48-48 48zM112 120c-30.928 0-56 25.072-56 56s25.072 56 56 56 56-25.072 56-56-25.072-56-56-56zM64 384h384V272l-87.515-87.515c-4.686-4.686-12.284-4.686-16.971 0L208 320l-55.515-55.515c-4.686-4.686-12.284-4.686-16.971 0L64 336v48z"></path></svg>',
- markdown: [''],
- textDefault: 'alt text',
- },
- /* Custom buttons */
- ];
- function addStyle(css) {
- const style = document.createElement('style');
- style.id = 'markdown-editor'
- style.innerHTML = css;
- document.head.appendChild(style);
- }
- function insertAtCursor (input, textToInsert, info) {
- input.focus();
- let sel1 = input.selectionStart;
- let sel2 = input.selectionEnd;
- const text = input.value;
- const breakLine = (info.newline === true && sel1 !== 0) ? '\n' : '';
- let sel = text.substring(sel1, sel2);
- if (sel.length === 0 && info.textDefault) {
- sel = info.textDefault;
- }
- document.execCommand('insertText', false, breakLine + textToInsert + sel);
- sel1 = sel1 + breakLine.length + textToInsert.length;
- sel2 = sel1 + sel.length;
- input.setSelectionRange(sel1, sel2);
- }
- function insertWrapAtCursor(input, tag1, tag2, info) {
- input.focus();
- let sel1 = input.selectionStart;
- let sel2 = input.selectionEnd;
- const text = input.value;
- const breakLine = (info.newline === true && sel1 !== 0) ? '\n' : '';
- let sel = text.substring(sel1, sel2);
- if (sel.length === 0 && info.textDefault) {
- sel = info.textDefault;
- }
- document.execCommand('insertText', false, breakLine + tag1 + sel + tag2);
- sel1 = sel1 + breakLine.length + tag1.length;
- sel2 = sel1 + sel.length;
- input.setSelectionRange(sel1, sel2);
- }
- function createButton(info) {
- const button = document.createElement('button');
- button.className = 'notes-custom';
- button.title = info.name;
- button.innerHTML = info.icon;
- button.tabIndex = -1;
- button.addEventListener('click', function (event) {
- event.stopPropagation();
- const noteEditor = document.querySelector('.cardview .NotesEditor-Editor');
- if (noteEditor) {
- if (Array.isArray(info.markdown)) {
- insertWrapAtCursor(noteEditor, info.markdown[0], info.markdown[1], info);
- } else if (info.wrap && typeof info.markdown === 'string') {
- insertWrapAtCursor(noteEditor, info.markdown, info.markdown, info);
- } else if (typeof info.markdown === 'function') {
- insertAtCursor(noteEditor, info.markdown(), info);
- } else {
- insertAtCursor(noteEditor, info.markdown, info);
- }
- }
- });
- return button;
- }
- function markdownEditor() {
- const addAttachmentsWrapper = document.querySelector('.NotesEditor > .toolbar:not([data-markdown-editor="true"])');
- if (addAttachmentsWrapper) {
- addAttachmentsWrapper.dataset.markdownEditor = true;
- buttons.forEach(function (buttonInfo) {
- addAttachmentsWrapper.append(createButton(buttonInfo));
- });
- }
- }
- addStyle([
- '.NotesEditor > .toolbar { flex-flow: row wrap; display: -webkit-box; }',
- '.NotesEditor > .toolbar .notes-custom { width: 28px; height: 28px; margin-right: 0; padding: 5px; border: 1px solid transparent; border-radius: var(--radius); background-color: transparent; background-image: linear-gradient(var(--colorBgLight), var(--colorBg)); }',
- '.NotesEditor > .toolbar .notes-custom:hover { background-color: var(--colorBgDark); }',
- '.NotesEditor > .toolbar .notes-custom svg { width: 16px; height: auto; fill: var(--colorFg); }',
- '.NotesEditor > .toolbar .notes-custom:hover svg { fill: var(--colorHighlightBg); }',
- '.NotesEditor > .toolbar .notes-toggle-md[disabled] ~ .notes-custom { opacity: 0.5; pointer-events: none; }',
- '.NotesEditor-Toolbar button.active { background-color: var(--colorFgFadedMore); }',
- ].join(''));
- const observeDOM = (function () {
- return function (obj, callback) {
- const obs = new MutationObserver(function (mutations, observer) {
- if (mutations[0].addedNodes.length || mutations[0].removedNodes.length) {
- callback(mutations, observer);
- }
- });
- obs.observe(obj, {
- childList: true,
- subtree: true
- });
- };
- })();
- observeDOM(document, function (mutations, observer) {
- const panels = document.querySelector('#panels');
- if (panels) {
- observer.disconnect();
- markdownEditor();
- observeDOM(panels, function () {
- markdownEditor();
- });
- }
- });
- })();
- /*
- * bookmark-panel-at-start
- * Written by luetage
- * GNU General Public License v3.0
- */
- setTimeout(function wait() {
- const book = document.querySelector('.panelbtn.mail');
- if (book) {
- if (!book.classList.contains('active')) book.click();
- }
- else setTimeout(wait, 300);
- }, 300)
Advertisement
Add Comment
Please, Sign In to add comment