Skip to content

Commit d2d2b17

Browse files
committed
feat: fix context menu paste and cache before paste
1 parent be5f61c commit d2d2b17

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

content-script.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ var tcl = {
4646
if (req.behavior != "pasteToTextarea") return;
4747
if (!req.skipConfirmPaste && !confirm(`paste "${req.val.slice(2)}" ?`)) return;
4848

49+
const e = document.activeElement
50+
if (!e) return
51+
tcl.saveToStorage(e)
4952
tcl.pasteToElement(req.val);
5053
});
5154
},
@@ -114,8 +117,10 @@ var tcl = {
114117
}
115118
},
116119

117-
saveToStorage: event => {
118-
var save_info = tcl.getContent(event.target);
120+
saveToStorage: x => {
121+
let e = x
122+
if (x instanceof Event) e = x.target
123+
var save_info = tcl.getContent(e);
119124

120125
if (strip(save_info.val).length == 0) return;
121126

@@ -125,7 +130,7 @@ var tcl = {
125130
title: window.parent.document.title,
126131
url: location.href,
127132
val: save_info.val,
128-
id: event.target.dataset['tcId'],
133+
id: e.dataset['tcId'],
129134
type: save_info.isWYSIWYG ? 'WYSIWYG' : 'txt',
130135
sessionKey: tcl.sessionKey
131136
});

ta_bg.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,31 @@ ta_bg.updateContext = async evt => {
131131
for (let i=0; i<datas.length; i++) {
132132
const o = datas[i]
133133
const o2 = {type: o.type, val: o.val}
134-
if (o.type == 'WYSIWYG') o2.val = domPurify.sanitize(o.val)
135134
menuItems.push(o2)
136135
}
137136
me.showCachesInContext(menuItems);
138137
};
139138

140-
ta_bg._menuOnClick = (info, tab) => {
139+
ta_bg.domPurify = null
140+
ta_bg.getDomPurify = async function () {
141+
if (this.domPurify) return this.domPurify
142+
const m = await import('./vendor/dompurify.js')
143+
return this.domPurify = m.default
144+
}
145+
146+
ta_bg._menuOnClick = async (info, tab) => {
141147
if (/^\[TEXTAREA CACHE\] /.test(info.menuItemId)) return;
142148

149+
const t = info.menuItemId.charAt(0)
150+
let b = info.menuItemId.slice(2)
151+
if (t == 'w') {
152+
const domPurify = await ta_bg.getDomPurify()
153+
b = domPurify.sanitize(b)
154+
}
155+
143156
tabs.sendMessage(tab.id, {
144157
behavior: 'pasteToTextarea',
145-
val: info.menuItemId,
158+
val: `${t}:${b}`,
146159
skipConfirmPaste: ta_database.data.setting.skipConfirmPaste
147160
});
148161
};

todo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
* maybe impossible
1111
* context menu
1212
* google chromium does not support menus.onShown event
13+
* remove confirm since we cache content before paste now

0 commit comments

Comments
 (0)