Skip to content

Instantly share code, notes, and snippets.

@whiteball
Last active January 28, 2023 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whiteball/0c4bcc19e50a4ab4bd91354918c1f248 to your computer and use it in GitHub Desktop.
Save whiteball/0c4bcc19e50a4ab4bd91354918c1f248 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name AIのべりすと Undo長押しで履歴の最初まで戻る
// @namespace https://ai-novelist-share.geo.jp/
// @version 0.1.1
// @description Undoボタンを1秒以上長押ししたとき、出力履歴を最初(出力が何もない状態)まで戻すようにします。
// @author しらたま
// @match https://ai-novel.com/novel.php
// @icon https://www.google.com/s2/favicons?sz=64&domain=ai-novel.com
// @updateURL https://gist.github.com/whiteball/0c4bcc19e50a4ab4bd91354918c1f248/raw/ai_novelist_undo_to_head.user.js
// @downloadURL https://gist.github.com/whiteball/0c4bcc19e50a4ab4bd91354918c1f248/raw/ai_novelist_undo_to_head.user.js
// @supportURL https://gist.github.com/whiteball/0c4bcc19e50a4ab4bd91354918c1f248
// @grant none
// ==/UserScript==
(function() {
'use strict';
const isSmartPhone = function () {
return navigator.userAgent.match(/iPhone|iPad|Android.+Mobile/i) || (navigator.userAgent.match(/Macintosh/i) && 'ontouchend' in document)
}
const undoElement = document.getElementById('undo')
let startTime = 0
const setStart = function () {
startTime = (new Date()).getTime()
}
undoElement.addEventListener('pointerdown', setStart)
const originUndoContent = window.UndoContent
window.UndoContent = function () {
if (startTime === 0) {
return
}
if (((new Date()).getTime() - startTime) >= 1000) {
localStorage.undo_last = 2
}
startTime = 0
originUndoContent()
}
if (isSmartPhone()) {
// スマホだとpointerdownで長押しした後にclickが発生しないので、pointerupにも登録する
undoElement.addEventListener('pointerup', window.UndoContent)
}
const targetNode = document.getElementById('retryoptions');
const observer = new MutationObserver(function(mutationsList, observer) {
for(const mutation of mutationsList) {
if (mutation.type === 'childList') {
if (document.getElementById('undo-origin')) {
// AIのべりすとユーティリティのUndo時の確認ダイアログとのバッティングを避けるために、ユーティリティで変更されたときは、ユーティリティによって追加されたボタンに対してイベント設定
const undo = document.getElementById('undo')
undo.addEventListener('pointerdown', setStart)
if (isSmartPhone()) {
// スマホだとpointerdownで長押しした後にclickが発生しないので、pointerupにも登録する
undo.addEventListener('pointerup', window.UndoContent)
}
observer.disconnect()
break
}
}
}
})
observer.observe(targetNode, { attributes: false, childList: true, subtree: true })
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment