之前用过很多的App来写作,最终,现在已经基本上转在notion上写作了,其他的已经不怎么用了,虽然是免费版本的,但是也基本上够用了,我认为大概有几个方面比较吸引我:
但是也有一些不爽的地方,好在也不是常用功能,比如
我最直接的方式是写一个chrome插件,给notion定制一下,让他有支持在编辑文档的时候可以插入图片的能力,后面了解了一些,还不如直接写一个油猴脚本。
如是,我先理了一下思路。
首先,我们需要看看这个油猴脚本包含哪些功能。
ok,说干就干,我把搜索界面设置为这样
我是用快捷键 ctrl + shift + i
唤起我们的表情搜索框。简单的实现如下:
document.addEventListener('keydown', function(event) {
if (
(event.ctrlKey || event.metaKey) &&
event.shiftKey &&
event.code === 'KeyI'
) {
console.log('Search image clicked');
createSearchPanel();
}
});
左上角,我放置了一个设置页面的入口,点击会弹出设置API key的页面,用户可以配置自己的apikey ,这是自己请求图片的凭据。是存放在本地的。
function saveApiKey() {
let apiKey = document.getElementById('api-key-input').value;
console.log('Saving API key:', apiKey);
//trim
apiKey = apiKey.trim();
//check apiKey
if (apiKey) {
GM_setValue('apiKey', apiKey);
alert('API Key saved successfully!');
} else {
alert('Please enter an API key.');
}
}
右上角,我放置了一个关闭按钮
,当然,你可以可以按 ESC,健退出图片搜索插件。
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
document.getElementById('imageSearchPanel').remove();
}
});
接下来就是需要做图片搜索了,
function searchImages(query) {
//console.log('Searching for:', query);
const apiKey = GM_getValue('apiKey', '');
if (!apiKey) {
alert('Please enter an API key,by clicking the settings button.');
return;
}
// search for images from giphy API
const url = `https://api.giphy.com/v1/gifs/search?api_key=${apiKey}&q=${encodeURIComponent(
query
)}&limit=20&rating=g&lang=zh-CN&bundle=messaging_non_clips`;
fetch(url)
.then(response => response.json())
.then(data => {
//console.log('API response:', data.data);
displayImages(data.data);
});
}
搜索到的图片显示在下面。
点击,图片,我预期是直接插入到notion的文档流当中,但是这个API我看了下,没找到路径,因此我直接复制图片地址了,这样粘贴到notion当中,也是ok的。
全部代码:https://gist.github.com/bravekingzhang/aa47fa43d1022bbcd1547979d3931e3a
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。