// ==UserScript== // @name Gitlab, open file in phpstorm // @namespace micoli.phpstorm.openlinks // @version 0.5 // @description add a link to phpstorm in gitlab ci merge requests files // @author You // @match https://gitlab.com/* // @downloadURL none // ==/UserScript== (function () { // defaults write com.google.Chrome URLWhitelist -array "phpstorm://* 'use strict'; let config = {}; let linkCounter = 0; document.querySelectorAll('a[href*="redirectFromReferer"]').forEach((link)=> { link.href = link.href+'?referer='+window.location.href }); const project = $('nav.breadcrumbs ul.js-breadcrumbs-list li a')[1].pathname; const loadPreferences = function () { if (!window.localStorage.getItem('openInPhpStormSettings')) { return {}; } config = JSON.parse(window.localStorage.getItem('openInPhpStormSettings')); } const savePreferences = function () { window.localStorage.setItem('openInPhpStormSettings', JSON.stringify(config)) removeLinks(); setLinks(); } const icon = function (color) { return ` ` }; const phpStormMimeLink = function(link){ const prefix = config[project] || ''; document.location = `phpstorm://open?file=${prefix}${link}`; }; document.phpStormRestApiLink = function (link) { const prefix = config[project] || ''; const hrefLink = `http://localhost:63342/api/file?file=/${prefix}${link}`; const linkWindow = window.open(hrefLink,'ee'); setTimeout(function(){ linkWindow.close(); },2); }; const openLinkInHiddenForm = function (action,parameters) { let hiddenDiv = document.getElementById('phpstorm-autoform'); if (hiddenDiv){ hiddenDiv.parentNode.removeChild(hiddenDiv); } console.log(action); document.querySelector('body').insertAdjacentHTML('beforeEnd', `
`); document.getElementById('phpstorm-autoform').submit(); }; const setLinks = function () { document.querySelectorAll('.file-header-content .file-title-name:not(.rgx-add-phpstorm-done)').forEach(function (link) { link.className = link.className + ' rgx-add-phpstorm-done'; const hrefLink = link.innerHTML.trim(); linkCounter++; link .closest('.js-file-title') .querySelector('div.file-header-content') .insertAdjacentHTML('beforeEnd', `
${icon()} `); document.getElementById(`phpstorm-${linkCounter}`).addEventListener('click',function(event){ document.phpStormRestApiLink(event.currentTarget.dataset.link); }); }); } const removeLinks = function () { document.querySelectorAll('.js-edit-in-phpstorm').forEach(function (v) { v.parentElement.removeChild(v); }); document.querySelectorAll('.file-header-content .file-title-name').forEach(function (v) { v.class = v.class.replace(/rgx-add-phpstorm-done/, ''); }); } const isConfigurationValid = function () { try { JSON.parse(document.getElementById("open-in-phpstorm-map").value); return true; } catch (e) { return false; } }; const togglePref = function () { var element = document.getElementById('open-in-phpstorm-settings'); element.style.display = element.style.display == 'none' ? 'inline-block' : 'none' if (element.style.display == 'inline-block') { document.getElementById("open-in-phpstorm-map").value = JSON.stringify(config); } }; const initPreferenceButton = function () { document.querySelector('.alert-wrapper').insertAdjacentHTML('afterEnd', `
`); document.querySelector('[data-qa-selector="settings_link"]').parentNode.insertAdjacentHTML('beforebegin', `
  • ${icon('#919191')} "Open in PhpStorm" Settings
  • `); document.getElementById('js-btn-open-in-phpstorm-settings').addEventListener('click', function () { togglePref(); return false; }); document.getElementById('js-btn-save-in-phpstorm-settings').addEventListener('click', function () { if (isConfigurationValid()) { togglePref(); config = JSON.parse(document.getElementById("open-in-phpstorm-map").value); savePreferences(); } return false; }); }; console.log('started'); loadPreferences(); setLinks(); window.setTimeout(initPreferenceButton, 50); window.setInterval(setLinks, 1500); })();