スワイプカーソル強化版
code:script.js
(function(){
// このスクリプトをオン・オフする
const this_script = 'on';
// 1本指スワイプ感度(数字が小さいほど感度が高い)
const sensitivity1 = 14;
// 2本指スワイプ感度
const sensitivity2 = 8;
// 1本指スワイプを有効にするデバイスのリスト(Dummy Deviceは消さないで下さい)
// 2本指スワイプを有効にするデバイスのリスト
// 拡張ポップアップに「コピー」を追加するデバイスのリスト
function get_coordinate($touchevent, num) {
return {
x: $touchevent.originalEvent.touchesnum.pageX, y: $touchevent.originalEvent.touchesnum.pageY };
}
function disable_scrolling() {
$(window).on('touchmove.no_scroll', e => e.preventDefault());
}
function enable_scrolling() {
$(window).off('.no_scroll');
}
function main(trial_times) {
try {
const textarea_elem = document.getElementById('text-input');
const $editor_obj = $('#editor');
if (!textarea_elem || !$editor_obj) {
throw new Error();
}
const keydown_event = {
event: document.createEvent('Event'),
init: function(type) {
this.event.initEvent(type, true, true);
},
dispatch: function(keycode, with_shift, elem) {
this.event.keyCode = keycode;
this.event.shiftKey = with_shift;
elem.dispatchEvent(this.event);
}
};
keydown_event.init('keydown');
const finger1_active = new RegExp(device_list1.join('|'), 'i').test(user_agent);
const finger2_active = new RegExp(device_list2.join('|'), 'i').test(user_agent);
let coord_start1, coord_start2;
let yx_ratio_threshold1 = 0.2;
let text_selected = false;
$editor_obj.on('touchstart', e => {
text_selected = false;
switch (e.originalEvent.touches.length) {
case 1:
coord_start1 = get_coordinate(e, 0);
break;
case 2:
if (!finger2_active || textarea_elem !== document.activeElement) return;
disable_scrolling();
coord_start1 = get_coordinate(e, 0);
coord_start2 = get_coordinate(e, 1);
break;
default:
break;
}
});
$editor_obj.on('touchmove', e => {
const coord_current1 = get_coordinate(e, 0);
const x_variation1 = coord_start1.x - coord_current1.x;
const y_variation1 = coord_start1.y - coord_current1.y;
switch (e.originalEvent.touches.length) {
case 1:
if (!finger1_active || textarea_elem !== document.activeElement || text_selected) return;
if ( Math.abs(y_variation1 / x_variation1) < yx_ratio_threshold1) {
if (x_variation1 > sensitivity1) {
keydown_event.dispatch(37, false, textarea_elem);
} else if (x_variation1 < - sensitivity1) {
keydown_event.dispatch(39, false, textarea_elem);
} else {
return;
}
disable_scrolling();
yx_ratio_threshold1 = 1;
coord_start1 = coord_current1;
}
break;
case 2:
if (!finger2_active || textarea_elem !== document.activeElement) return;
const coord_current2 = get_coordinate(e, 1);
const x_variation2 = coord_start2.x - coord_current2.x;
const x_variation_sum = x_variation1 + x_variation2;
if (x_variation_sum > sensitivity2) {
keydown_event.dispatch(37, true, textarea_elem);
} else if (x_variation_sum < - sensitivity2) {
keydown_event.dispatch(39, true, textarea_elem);
} else {
return;
}
text_selected = true;
coord_start1 = coord_current1;
coord_start2 = coord_current2;
break;
default:
break;
}
});
$editor_obj.on('touchend', e => {
enable_scrolling();
yx_ratio_threshold1 = 0.2;
});
} catch(e) {
trial_times++;
if (trial_times > 10) return;
setTimeout(() => {
main(trial_times);
}, 2000);
}
}
const user_agent = navigator.userAgent;
if (this_script === 'on') {
if (new RegExp(device_list3.join('|'), 'i').test(user_agent)) {
scrapbox.PopupMenu.addButton({
title: 'コピー',
onClick: () => document.execCommand('copy')
});
}
if (new RegExp(device_list1.concat(device_list2).join('|'), 'i').test(user_agent)) {
main(0);
}
}
})();