Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
web-monkey-scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hugo Hörnquist
web-monkey-scripts
Commits
27477e5f
Commit
27477e5f
authored
2 years ago
by
Hugo Hörnquist
Browse files
Options
Downloads
Patches
Plain Diff
Add date-button.
parent
d69a925c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
date-button/index.js
+124
-0
124 additions, 0 deletions
date-button/index.js
with
124 additions
and
0 deletions
date-button/index.js
0 → 100644
+
124
−
0
View file @
27477e5f
// ==UserScript==
// @name Date Button
// @namespace http://hugo.hornquist.se
// @version 0.1
// @author hugo@lysator.liu.se
// @match *
// @description Inputs the current date into the current text field.
// @updateURL https://git.lysator.liu.se/hugo/web-monkey-scripts/raw/master/date-button/index.js
// @downloadURL https://git.lysator.liu.se/hugo/web-monkey-scripts/raw/master/date-button/index.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// ==/UserScript==
(
function
()
{
'
use strict
'
;
function
serialize_event
(
e
)
{
return
{
'
key
'
:
e
.
key
,
'
ctrl
'
:
e
.
ctrlKey
,
'
alt
'
:
e
.
altKey
,
'
shift
'
:
e
.
shiftKey
,
}
}
function
deserialize_event
(
data
)
{
return
new
KeyboardEvent
(
'
keydown
'
,
{
ctrlKey
:
data
.
ctrl
,
altKey
:
data
.
alt
,
shiftKey
:
data
.
shift
,
key
:
data
.
key
,
})
}
function
event_matches
(
a
,
b
)
{
return
a
.
ctrlKey
==
b
.
ctrlKey
&&
a
.
altKey
==
b
.
altKey
&&
a
.
shiftKey
==
b
.
shiftKey
&&
a
.
key
==
b
.
key
;
}
function
formatKeyEvent
(
e
)
{
let
s
=
''
;
if
(
e
.
ctrlKey
)
s
+=
'
Ctrl-
'
;
if
(
e
.
altKey
)
s
+=
'
Alt-
'
;
if
(
e
.
shiftKey
)
s
+=
'
Shift-
'
;
if
(
e
.
key
!=
'
Shift
'
&&
e
.
key
!=
'
Control
'
&&
e
.
key
!=
'
Alt
'
)
s
+=
e
.
key
;
return
s
;
}
let
btn
=
document
.
createElement
(
'
button
'
);
btn
.
style
.
position
=
'
absolute
'
;
btn
.
style
.
top
=
'
1em
'
;
btn
.
style
.
left
=
'
1em
'
;
let
registered_key_combo
=
GM_getValue
(
'
date-key
'
);
console
.
log
(
registered_key_combo
);
if
(
registered_key_combo
==
null
)
{
btn
.
textContent
=
'
Register keycode
'
}
else
{
registered_key_combo
=
deserialize_event
(
registered_key_combo
)
btn
.
textContent
=
formatKeyEvent
(
registered_key_combo
);
}
console
.
log
(
registered_key_combo
);
btn
.
addEventListener
(
'
click
'
,
(
e
)
=>
{
btn
.
textContent
=
'
Input key sequence ...
'
})
document
.
body
.
appendChild
(
btn
);
let
state
=
false
btn
.
addEventListener
(
'
keydown
'
,
(
e
)
=>
{
console
.
log
(
e
);
let
s
=
formatKeyEvent
(
e
)
console
.
log
(
s
);
btn
.
textContent
=
s
state
=
'
down
'
})
btn
.
addEventListener
(
'
keyup
'
,
(
e
)
=>
{
if
(
state
!=
'
down
'
)
return
;
state
=
'
up
'
registered_key_combo
=
e
;
GM_setValue
(
'
date-key
'
,
serialize_event
(
e
))
btn
.
blur
();
e
.
preventDefault
();
})
document
.
addEventListener
(
'
keydown
'
,
(
e
)
=>
{
if
(
event_matches
(
e
,
registered_key_combo
))
{
if
(
!
document
.
activeElement
)
return
;
let
start
=
document
.
activeElement
.
selectionStart
;
let
end
=
document
.
activeElement
.
selectionEnd
;
if
(
start
==
undefined
||
end
==
undefined
)
return
;
let
s
=
document
.
activeElement
.
value
s
=
s
.
substring
(
0
,
start
)
+
(
new
Date
).
toISOString
()
+
s
.
substring
(
end
)
document
.
activeElement
.
value
=
s
;
e
.
preventDefault
();
}
});
let
style
=
document
.
createElement
(
'
style
'
);
style
.
textContent
=
`button:focus {background: pink;})`
document
.
head
.
appendChild
(
style
);
})();
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment