Skip to content

Commit

Permalink
basic scripts cleanup
Browse files Browse the repository at this point in the history
- moving general dev notes to README.md
- more accurate user help
- fixing hideCommandsOverlay() - it didn't clear existing timeout
- using apostrophes consistently and some other code cleanup
  • Loading branch information
pbodnar committed Apr 4, 2017
1 parent 8a236c8 commit 20772b4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
60 changes: 30 additions & 30 deletions JIRA_copy.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@
// @require https://raw.githubusercontent.com/jeresig/jquery.hotkeys/0.2.0/jquery.hotkeys.js
// ==/UserScript==

// NOTES FOR DEVS
// http://stackoverflow.com/questions/9931115/run-greasemonkey-on-html-files-located-on-the-local-filesystem
// -> about:config -> greasemonkey.fileIsGreaseable: true

var traceOn = true;

trace("starting...");
trace('starting...');

function showHelp() {
alert(`Copy Anything for JIRA - Help
Press 'd' to copy various parts of the page into the clipboard.
Press 'D' to display the follow-up options to copy various parts of the page into the clipboard.
@author pbodnar
@version 1.0.0
`);
}

try {
trace("command register");
GM_registerMenuCommand("Copy Anything for JIRA - Help", showHelp);
trace("command registered");
trace('command register');
GM_registerMenuCommand('Copy Anything for JIRA - Help', showHelp);
trace('command registered');
} catch (e) {
trace("command register error: " + e);
trace('command register error: ' + e);
}

// ==================== JIRA copy commands ====================
Expand Down Expand Up @@ -80,26 +76,29 @@ function showCommandsOverlay(event) {

function hideCommandsOverlay() {
commandsEnabled = false;
timer = null;
if (timer) {
clearTimeout(timer);
timer = null;
}
var overlay = getCommandsDiv();
$(overlay).hide();
}

function getCommandsDiv() {
if (typeof(commandsDiv) != "undefined") {
if (typeof commandsDiv != 'undefined') {
return commandsDiv;
}
var div = commandsDiv = document.createElement('div');
div.style = 'z-index: 1000001; color: red; background-color: white; padding: 1em; border: 1px solid red;';
div.style.position = 'absolute';
div.style.opacity = 0.75;
div.innerHTML = `Shortcuts available for copying to clipboard as:
div.innerHTML = `Follow-up shortcuts available for copying issue details to clipboard (<key> ... <what appears in clipboard>):
<ul>
<li>shift+l ... <u>l</u>ink to issue, including issue key and summary</li>
<li>shift+s ... <u>s</u>hort link to issue, including just issue key</li>
<li>shift+m ... commit <u>m</u>essage template</li>
<li>Shift+L ... <u>l</u>ink to issue, including issue key and summary</li>
<li>Shift+S ... <u>s</u>hort link to issue, including just issue key</li>
<li>Shift+M ... commit <u>m</u>essage template</li>
<hr />
<li>shift+e ... <u>e</u>scape - hide this popup</li>
<li>Shift+E ... <u>e</u>scape - hide this popup</li>
</ul>`;

insertBodyElement(div);
Expand All @@ -111,9 +110,9 @@ function getCommandsDiv() {

function bindShortcut(keys, handler) {
// make the shortcuts case-insensitive (bind() takes space as alternative keys separator):
keys = keys + " " + keys.toUpperCase();
keys = keys + ' ' + keys.toUpperCase();

$(document).bind("keypress", keys, handler);
$(document).bind('keypress', keys, handler);
}

function bindCopyShortcut(keys, handler) {
Expand All @@ -126,6 +125,7 @@ function bindCopyShortcut(keys, handler) {
});
}

// TODO How to temporatily suppress the standard JIRA shortcuts, so there is no need to combine the letters with for example 'shift' as below?
bindShortcut('d', showCommandsOverlay);
bindCopyShortcut('shift+e', hideCommandsOverlay);

Expand Down Expand Up @@ -158,7 +158,7 @@ function showInfo(html) {
}

function getInfoDiv() {
if (typeof(infoDiv) != "undefined") {
if (typeof infoDiv != 'undefined') {
return infoDiv;
}
var div = infoDiv = document.createElement('div');
Expand All @@ -176,7 +176,7 @@ function getInfoDiv() {
// Inspired by http://jsfiddle.net/73v73p18/

function getClipboardDiv() {
if (typeof(clipboardDiv) != "undefined") {
if (typeof clipboardDiv != 'undefined') {
return clipboardDiv;
}
clipboardDiv = document.createElement('div');
Expand Down Expand Up @@ -208,7 +208,7 @@ function getClipboardDiv() {
function copyHtmlToClipboard(html) {
var clipboardDiv = getClipboardDiv();
// Note: adding redundant <span> element in order not to have any trailing new line or other white space in the clipboard
clipboardDiv.innerHTML = html + "<span></span>";
clipboardDiv.innerHTML = html + '<span></span>';

var focused = document.activeElement;
clipboardDiv.focus();
Expand All @@ -230,7 +230,7 @@ function copyHtmlToClipboard(html) {
focused.focus();
}

trace("started");
trace('started');

// ==================== Generic functions ====================

Expand All @@ -250,11 +250,11 @@ function getPagePath() {
var loc = window.location.href;

// cut off any '?...' if present
var qIndex = loc.indexOf("?");
var qIndex = loc.indexOf('?');
loc = qIndex > -1 ? loc.substring(0, qIndex) : loc;

// cut off any #...' if present
var hIndex = loc.indexOf("#");
var hIndex = loc.indexOf('#');
loc = hIndex > -1 ? loc.substring(0, hIndex) : loc;

return loc;
Expand All @@ -275,10 +275,10 @@ function trace(text) {

// http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen
jQuery.fn.center = function () {
this.css("position", "absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + "px");
this.css('position', 'absolute');
this.css('top', Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + 'px');
this.css('left', Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + 'px');
return this;
}
23 changes: 10 additions & 13 deletions JIRA_shortcuts.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,35 @@
// @require https://raw.githubusercontent.com/jeresig/jquery.hotkeys/0.2.0/jquery.hotkeys.js
// ==/UserScript==

// IMPORTANT: At least some @grant <> 'none' must be present to force GM to run the script 'in sandbox'.
// See http://stackoverflow.com/questions/12146445/jquery-in-greasemonkey-1-0-conflicts-with-websites-using-jquery

var traceOn = true;

trace('starting...');

function showHelp() {
alert(`Shortcuts for JIRA - Help
This adds some keyboard shortcuts which are missing in the default JIRA instalation.
Just one hard-coded shortcut is available now (<key> ... <action>):
This adds some keyboard shortcuts which are missing in the default JIRA installation.
Just one hard-coded shortcut is available for now (<key> ... <action>):
- w ... show the Log Work dialog
- W ... show the Log Work dialog (on the issue detail page)
@author pbodnar
@version 1.0.0
`);
}

try {
trace("command register");
GM_registerMenuCommand("Shortcuts for JIRA - Help", showHelp);
trace("command registered");
trace('command register');
GM_registerMenuCommand('Shortcuts for JIRA - Help', showHelp);
trace('command registered');
} catch (e) {
trace("command register error: " + e);
trace('command register error: ' + e);
}

// ==================== Commands ====================

function showLogWorkDialog() {
var btn = $('#stalker #log-work')[0];
var btn = $('#log-work')[0];
if (!btn) {
trace('The Log Work button not found!');
return;
Expand All @@ -56,9 +53,9 @@ function showLogWorkDialog() {
function bindShortcut(keys, handler) {
// make the shortcuts case-insensitive (bind() takes space as alternative keys
// separator):
keys = keys + " " + keys.toUpperCase();
keys = keys + ' ' + keys.toUpperCase();

$(document).bind("keypress", keys, handler);
$(document).bind('keypress', keys, handler);
}

bindShortcut('w', showLogWorkDialog);
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ The usage is always specific to a given user script. Most user scripts should of
What if something is not working as expected? Then you should firstly try to find some relevant information or error messages in the console log - this is typically available by pressing F12 in your browser and by navigating to the "Console" tab. And select for example "Logging -> Log" to see detailed logging from the GM scripts in Firefox.

If you find a bug or a missing feature, you can create a new issue for that on the "Issues" page of this Github project.

# For developers

This special chapter includes tips for those who would like to develop GM / TM scripts.

## Run GM Scripts on Local Disk Files

Set the following on the "about:config" page in Firefox:

greasemonkey.fileIsGreaseable: true

See http://stackoverflow.com/questions/9931115/run-greasemonkey-on-html-files-located-on-the-local-filesystem

## Force GM to Run Script in Sandbox

At least some `@grant <> 'none'` must be present in the user script's header in order to force GM to run the script 'in sandbox'.

See http://stackoverflow.com/questions/12146445/jquery-in-greasemonkey-1-0-conflicts-with-websites-using-jquery

0 comments on commit 20772b4

Please sign in to comment.