tmiyama
https://gyazo.com/d8bd016779206f2f3d76e78de31f8ced
this is tmiyama’s page
Senior Scientist of Application laboratory in JAMSTEC
類似したタイトルのページを関連ページとして表示する - Scrapbox研究会
code:script.js
(() => {
const Asearch=function(){var t,i,s;function p(i){var p,r,n,o,e,h,u;for(this.source=i,this.shiftpat=[],this.epsilon=0,this.acceptpat=0,e=t,p=r=0,h=s;0<=h?r<h:r>h;p=0<=h?++r:--r)this.shiftpatp=0;for(n=0,o=(u=this.unpack(this.source)).length;n<o;n++)32===(p=un)?this.epsilon|=e:(this.shiftpatp|=e,this.shiftpatthis.toupper(p)|=e,this.shiftpatthis.tolower(p)|=e,e>>>=1);return this.acceptpat=e,this}return s=256,i=t=2147483648,0,0,0,p.prototype.isupper=function(t){return t>=65&&t<=90},p.prototype.islower=function(t){return t>=97&&t<=122},p.prototype.tolower=function(t){return this.isupper(t)?t+32:t},p.prototype.toupper=function(t){return this.islower(t)?t-32:t},p.prototype.state=function(t,s){var p,r,n,o,e,h,u,a,c;for(null==t&&(t=i),null==s&&(s=""),n=t0,o=t1,e=t2,h=t3,r=0,u=(c=this.unpack(s)).length;r<u;r++)p=cr,a=this.shiftpatp,h=h&this.epsilon|(h&a)>>>1|e>>>1|e,e=e&this.epsilon|(e&a)>>>1|o>>>1|o,o=o&this.epsilon|(o&a)>>>1|n>>>1|n,h|=(e|=(o|=(n=n&this.epsilon|(n&a)>>>1)>>>1)>>>1)>>>1;returnn,o,e,h},p.prototype.match=function(t,s){var p;return null==s&&(s=0),p=this.state(i,t),s<i.length||(s=i.length-1),0!=(ps&this.acceptpat)},p.prototype.unpack=function(t){var i,s,p,r,n;for(i=[],p=0,r=(n=t.split("")).length;p<r;p++)(s=np.charCodeAt(0))>255&&i.push((65280&s)>>>8),i.push(255&s);return i},p}(); if ($('#asearch-list-grid').length !== 0) {
return;
}
$('.page-wrapper .related-page-list.clearfix').append(
'<ul class="grid" id="asearch-list-grid"><li class="splitter" /><li class="relation-label">' +
'<a><span class="title">Similar Pages</span>' +
'<span class="kamon kamon-search icon-lg"/></a><span class="arrow"/></li></ul>'
);
const grid = $('#asearch-list-grid');
let prevTitle;
function resetList() {
grid.children().slice(2).remove();
}
function pathToTitle(path) {
const a = path.split('/');
if (a.length === 3 && a2.length > 0) { const title = decodeURIComponent(a2); // Project.pagesを見に行く
// (_が実際は空白なのか_なのか分からないため)
const titleLc = title.toLowerCase();
const p = scrapbox.Project.pages.find((p) => p.titleLc === titleLc);
return p ? p.title : title;
}
return null;
}
function updateList(title, replace) {
// 既に関連リンクに入っているページのタイトルを取得する
let links = new Set();
$('.page-wrapper .related-page-list.clearfix ul.grid:nth(0) li a').each(
(i, e) => {
links.add(pathToTitle(e.pathname));
}
);
const search = new Asearch(title);
const titleLc = title.toLowerCase();
for (let p of scrapbox.Project.pages) {
if (p.title === title || links.has(p.title)) continue;
if (replace && p.title === prevTitle) continue;
const pTitleLc = p.title.toLowerCase();
if (
search.match(p.title, Math.min(title.length - 1, 2)) ||
pTitleLc.indexOf(titleLc) != -1 ||
titleLc.indexOf(pTitleLc) != -1
) {
const item = $(`<li class="page-list-item grid-style-item${
p.exists ? '' : ' empty'
}">
<a href="/${scrapbox.Project.name}/${encodeURIComponent(p.title.replace(' ', '_'))}" rel="route">
<div class="hover"></div>
<div class="content">
<div class="header"><div class="title"></div></div>
<div class="description"><div class="line-img">
<div></div><div></div><div></div><div></div><div></div>
</div></div>
</div>
</a>`);
$('.title', item).text(p.title);
grid.append(item);
}
grid.show();
}
}
// タイトル行が書き換わるのを監視
//(その時点でページ書き換えが完了していることを期待)
function waitForUpdate(title, n, replace) {
if ($('.lines .line:nth(0)').text() === title) {
resetList();
updateList(title, replace);
prevTitle = title;
return;
}
if (n == 0) {
resetList();
return;
}
setTimeout(() => waitForUpdate(title, n - 1, replace), 100);
}
function hook(func) {
const history = window.history;
const orig = historyfunc; historyfunc = function (state) { let title = pathToTitle(state.path);
if (title === 'new') {
resetList();
} else if (title) {
waitForUpdate(title, 50, func === 'replaceState');
}
return orig.apply(history, arguments);
};
}
hook('pushState');
hook('replaceState');
let title = pathToTitle(location.pathname);
if (title) waitForUpdate(title, 20);
})();