-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpaste-webpage-url.js
More file actions
111 lines (100 loc) · 3.21 KB
/
Copy pathpaste-webpage-url.js
File metadata and controls
111 lines (100 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const $ = require("jquery");
const { isChrome, isFirefox } = require("../browser");
const daiizScrapboxManage = require("./manage");
const { installed } = daiizScrapboxManage;
const keys = {
ctrl: 17,
alt: 18,
v: 86,
};
// Clipboardに保持されたURLのページタイトルを返却する
const execPasteFirefox = async () => {
// background scriptに処理を依頼できない
// 拡張機能用のtextareaを生成してbody末尾に挿入
const textInput = document.querySelector("#text-input");
let textarea = document.querySelector("textarea#daiiz-ctrlv");
if (!textarea) {
textarea = document.createElement("textarea");
textarea.setAttribute("id", "daiiz-ctrlv");
document.body.appendChild(textarea);
}
// clipboardが保持する内容を流し込んで値を取得
let rawText;
const onPaste = (event) => {
rawText = event.clipboardData.getData("text/plain");
// fetch APIの実行してtextを解決する処理はbackground scriptに依頼する
const payload = {
command: "fetch-page-title",
rawText,
};
window.app.runtime.sendMessage(payload, (text) => {
if (!text) {
return;
}
insertTextToScrapboxCursor(text);
});
};
textarea.value = "";
textarea.focus();
document.addEventListener("paste", onPaste, false);
textInput.disabled = true;
document.execCommand("paste");
textInput.disabled = false;
document.removeEventListener("paste", onPaste, false);
textarea.remove();
};
const insertTextToScrapboxCursor = (text) => {
// Scrapboxで入力を待ち受けているtextarea要素
const textInput = document.querySelector("#text-input");
textInput.disabled = false;
if (isChrome()) {
textInput.focus();
document.execCommand("insertText", false, text);
} else {
// Firefoxでは document.execCommand('insertText') が使えない
// 代わりに自前で生成したUIEventを発行すればいい
// https://www.everythingfrontend.com/posts/insert-text-into-textarea-at-cursor-position.html
const start = textInput.selectionStart; // in this case maybe 0
textInput.setRangeText(text);
textInput.selectionStart = textInput.selectionEnd = start + text.length;
const uiEvent = document.createEvent("UIEvent");
uiEvent.initEvent("input", true, false);
textInput.dispatchEvent(uiEvent);
}
};
exports.enable = () => {
let c = 0;
$(window).on("keydown", (event) => {
const key = installed("daiiz-paste-url-title");
if (!key) {
return;
}
const { keyCode } = event;
if (keyCode === keys[key]) c = 1;
});
$(window).on("keydown", (event) => {
if (!installed("daiiz-paste-url-title")) {
return;
}
const { keyCode } = event;
if (keyCode !== keys.v || c !== 1) {
return;
}
event.preventDefault();
event.stopPropagation();
if (isChrome()) {
execPasteFirefox();
} else if (isFirefox()) {
execPasteFirefox();
}
});
$(window).on("keyup", () => {
c = 0;
});
window.app.runtime.onMessage.addListener((request, sender, sendResponse) => {
const { command, externalLink } = request;
if (command === "re:get-clipboard-page") {
insertTextToScrapboxCursor(externalLink);
}
});
};