-
Notifications
You must be signed in to change notification settings - Fork 3
/
searchwithgyazo.user.js
101 lines (94 loc) · 3.56 KB
/
searchwithgyazo.user.js
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
// ==UserScript==
// @grant GM_xmlhttpRequest
// @name Search with Gyazo
// @namespace http://tampermonkey.net/
// @version 5.0
// @description try to take over the world!
// @author You
// @match https://scrapbox.io/*
// ==/UserScript==
const GM_get = (url)=>{
return new Promise((r)=>{
const method = "GET";
const onload = (res)=> r(JSON.parse(res.responseText));
GM_xmlhttpRequest({ method, url, onload });
});
}
(function() {
'use strict';
const searchArticleAndAppend = async ({image_id}, pages, lineIds)=>{
const prj = location.pathname.split("/")[1];
const url = `https://scrapbox.io/api/pages/${prj}/search/query?skip=0&sort=updated&limit=30&q=${image_id}`;
const data = await (await fetch(url)).json();
if(data.pages.length > 0){
for(const page of data.pages){
console.log(page);
if(!pages[page.title]){
pages[page.title] = await (await fetch(`https://scrapbox.io/api/pages/${prj}/${encodeURIComponent(page.title)}?followRename=true')`)).json();
}
const line = pages[page.title].lines.find(x => x.text.match(image_id));
if(lineIds.indexOf(line.id) === -1){
console.log('koko');
lineIds.push(line.id);
const li = jQuery(`
<li class="page-list-item list-style-item">
<a href="/${prj}/${encodeURIComponent(page.title)}#${line.id}" rel="route" style="display:flex" target='_blank'>
<div style="padding-right:20px">
<img loading="lazy" src="http://gyazo.com/${image_id}/raw" style="width:250px;max-height:400px">
</div>
<div style="flex:1">
<div class="title-with-description">${page.title}</div>
<div class="description"><span>${page.descriptions[0]}</span>
</div>
</div>
</a>
</li>
`);
jQuery('#searchWithGyazo').append(li);
}
}
}
}
const search = async ()=>{
const query = decodeURIComponent(location.search.replace("?q=", ""))
const url = `https://gyazo.com/api/internal/search_result?page=1&per=40&query=${encodeURIComponent(query)}`;
const data = await GM_get(url);
const pages = {};
const lineIds = [];
data.captures.forEach(cap => searchArticleAndAppend(cap, pages, lineIds))
jQuery('#gyazobutton').remove();
}
const main = ()=>{
if(location.href.match(/search\/page/)){
const element = jQuery('<div id="addiction" class="project-search">');
element.append('<div class="project-search-count">Search with Gyazo</div>')
const formDiv = jQuery('<div class="text-center"></div>')
const button = jQuery('<button id="gyazobutton" type="submit" class="project-search-button btn btn-auto-block btn-default">Search with Gyazo</button>')
button.click(search);
formDiv.append(button)
element.append(formDiv)
element.append('<ul class="list" id="searchWithGyazo" style="padding-bottom: 15px"></ul>')
jQuery('.project-search').before(element);
}
}
// ページ遷移を雑にループで検知
const loop = (href)=>{
setTimeout(()=>{
if(location.href !== href){
document.querySelectorAll('#addiction').forEach(n => n.remove());
initialize();
}
loop(location.href);
}, 200);
};
loop(location.href);
// 読み込み完了を待つ
const initialize = ()=>{
if(document.querySelector('.project-home')){
main();
} else {
setTimeout(initialize, 500);
}
}
initialize();
})();