aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2021-03-21 16:48:32 +0100
committerTeddy Wing2021-03-21 17:45:12 +0100
commit34bcab5f0aacee38d55d11821073c878ca51e923 (patch)
treecd0530a6958abc94f31474c0a69a8f11cd6bb0a9
parentdb45d89fc0acf1999c2b996397d2872159c0b74c (diff)
downloadPeniquitous-34bcab5f0aacee38d55d11821073c878ca51e923.tar.bz2
Makefile: Add build rules to build dependencies into main.js
Instead of copy-pasting the code from the files directly into main.js, do it in the build recipes.
-rw-r--r--.gitignore1
-rw-r--r--Makefile57
-rw-r--r--main.js153
3 files changed, 51 insertions, 160 deletions
diff --git a/.gitignore b/.gitignore
index 2ccbe46..34b23da 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
+/build/
/node_modules/
diff --git a/Makefile b/Makefile
index 4bc90c8..c557f76 100644
--- a/Makefile
+++ b/Makefile
@@ -16,17 +16,56 @@
BROWSERIFY := ./node_modules/.bin/browserify
+IMPORTS := peniquitous.js lib/mousetrap/tests/libs/key-event.js
+IMPORTS_BASENAME := $(notdir $(IMPORTS))
+
all: peniquitous.user.js
-peniquitous.user.js: peniquitous.js lib/mousetrap/tests/libs/key-event.js userscript-header.txt
- sed -e '1d' -e '$$d' \
- lib/mousetrap/tests/libs/key-event.js \
- > key-event.js
+# peniquitous.user.js: peniquitous.js lib/mousetrap/tests/libs/key-event.js userscript-header.txt
+# sed -e '1d' -e '$$d' \
+# lib/mousetrap/tests/libs/key-event.js \
+# > key-event.js
+#
+# cat \
+# userscript-header.txt \
+# key-event.js \
+# peniquitous.js \
+# > $@
+#
+# rm key-event.js
+
+# build/?: $(IMPORTS)
+# sed -e '/^(function/d' -e '$$d' file
+#
+# build/main.js: build/*.js
+
+build:
+ mkdir -p $@
+
+build/key-event.js: lib/mousetrap/tests/libs/key-event.js | build
+ sed -e '/^(function/d' -e '$$d' $< > $@
+
+build/peniquitous.js: peniquitous.js | build
+ sed -e '/^(function/d' -e '$$d' $< > $@
- cat \
- userscript-header.txt \
- key-event.js \
- peniquitous.js \
+# TODO: Swap main file names
+main.js.in: $(addprefix build/,$(IMPORTS_BASENAME))
+ sed \
+ -e '/\$$KEY_EVENT/r build/key-event.js' \
+ -e '/\$$KEY_EVENT/d' \
+ -e '/\$$PENIQUITOUS/r build/peniquitous.js' \
+ -e '/\$$PENIQUITOUS/d' \
+ main.js \
> $@
- rm key-event.js
+peniquitous.user.js: main.js peniquitous.js userscript-header.txt
+ $(BROWSERIFY) \
+ --outfile $@ \
+ $<
+
+ cat userscript-header.txt $@ > "$@.tmp"
+ mv "$@.tmp" $@
+
+.PHONY: clean
+clean:
+ rm -rf build
diff --git a/main.js b/main.js
index 927dea6..1079a75 100644
--- a/main.js
+++ b/main.js
@@ -18,160 +18,11 @@
// var peniquitous = require('./peniquitous');
function key_event () {
- var KeyEvent = function(data, type) {
- this.keyCode = 'keyCode' in data ? data.keyCode : 0;
- this.charCode = 'charCode' in data ? data.charCode : 0;
-
- var modifiers = 'modifiers' in data ? data.modifiers : [];
-
- this.ctrlKey = false;
- this.metaKey = false;
- this.altKey = false;
- this.shiftKey = false;
-
- for (var i = 0; i < modifiers.length; i++) {
- this[modifiers[i] + 'Key'] = true;
- }
-
- this.type = type || 'keypress';
- };
-
- KeyEvent.prototype.toNative = function() {
- var event = document.createEventObject ? document.createEventObject() : document.createEvent('Events');
-
- if (event.initEvent) {
- event.initEvent(this.type, true, true);
- }
-
- event.keyCode = this.keyCode;
- event.which = this.charCode || this.keyCode;
- event.shiftKey = this.shiftKey;
- event.metaKey = this.metaKey;
- event.altKey = this.altKey;
- event.ctrlKey = this.ctrlKey;
-
- return event;
- };
-
- KeyEvent.prototype.fire = function(element) {
- var event = this.toNative();
- if (element.dispatchEvent) {
- element.dispatchEvent(event);
- return;
- }
-
- element.fireEvent('on' + this.type, event);
- };
-
- // simulates complete key event as if the user pressed the key in the browser
- // triggers a keydown, then a keypress, then a keyup
- KeyEvent.simulate = function(charCode, keyCode, modifiers, element, repeat) {
- if (modifiers === undefined) {
- modifiers = [];
- }
-
- if (element === undefined) {
- element = document;
- }
-
- if (repeat === undefined) {
- repeat = 1;
- }
-
- var modifierToKeyCode = {
- 'shift': 16,
- 'ctrl': 17,
- 'alt': 18,
- 'meta': 91
- };
-
- // if the key is a modifier then take it out of the regular
- // keypress/keydown
- if (keyCode == 16 || keyCode == 17 || keyCode == 18 || keyCode == 91) {
- repeat = 0;
- }
-
- var modifiersToInclude = [];
- var keyEvents = [];
-
- // modifiers would go down first
- for (var i = 0; i < modifiers.length; i++) {
- modifiersToInclude.push(modifiers[i]);
- keyEvents.push(new KeyEvent({
- charCode: 0,
- keyCode: modifierToKeyCode[modifiers[i]],
- modifiers: modifiersToInclude
- }, 'keydown'));
- }
-
- // @todo factor in duration for these
- while (repeat > 0) {
- keyEvents.push(new KeyEvent({
- charCode: 0,
- keyCode: keyCode,
- modifiers: modifiersToInclude
- }, 'keydown'));
-
- keyEvents.push(new KeyEvent({
- charCode: charCode,
- keyCode: charCode,
- modifiers: modifiersToInclude
- }, 'keypress'));
-
- repeat--;
- }
-
- keyEvents.push(new KeyEvent({
- charCode: 0,
- keyCode: keyCode,
- modifiers: modifiersToInclude
- }, 'keyup'));
-
- // now lift up the modifier keys
- for (i = 0; i < modifiersToInclude.length; i++) {
- var modifierKeyCode = modifierToKeyCode[modifiersToInclude[i]];
- modifiersToInclude.splice(i, 1);
- keyEvents.push(new KeyEvent({
- charCode: 0,
- keyCode: modifierKeyCode,
- modifiers: modifiersToInclude
- }, 'keyup'));
- }
-
- for (i = 0; i < keyEvents.length; i++) {
- // console.log('firing', keyEvents[i].type, keyEvents[i].keyCode, keyEvents[i].charCode);
- keyEvents[i].fire(element);
- }
- };
-
- window.KeyEvent = KeyEvent;
+ $KEY_EVENT
}
function peniquitous () {
- key_codes = {
- p: 80,
- n: 78,
- UP_ARROW: 38,
- DOWN_ARROW: 40
- };
-
- // Additional types:
- // * email
- // * number
- // * tel
- // * url
- var all_inputs = document.querySelectorAll('input[type="text"], input[type="search"]');
-
- for (var i = 0; i < all_inputs.length; i++) {
- all_inputs[i].addEventListener('keyup', function(e) {
- if (e.ctrlKey && e.keyCode === key_codes.p) {
- KeyEvent.simulate(0, key_codes.UP_ARROW, [], e.target);
- }
- else if (e.ctrlKey && e.keyCode === key_codes.n) {
- KeyEvent.simulate(0, key_codes.DOWN_ARROW, [], e.target);
- }
- });
- }
+ $PENIQUITOUS
}
(function() {