Skip to content

Instantly share code, notes, and snippets.

@htsign
Last active March 23, 2023 12:34
Show Gist options
  • Save htsign/b2c913f28e4b8646d0b30c4170e7d34c to your computer and use it in GitHub Desktop.
Save htsign/b2c913f28e4b8646d0b30c4170e7d34c to your computer and use it in GitHub Desktop.
はてブの「○○ users」を Bing に追加する
// ==UserScript==
// @name Hatena bookmark compatibility for Bing
// @namespace https://htsign.hateblo.jp
// @version 0.0.3
// @description add Hatebu images for Bing
// @author htsign
// @match https://www.bing.com/search?*
// @downloadURL https://gist.github.com/htsign/b2c913f28e4b8646d0b30c4170e7d34c/raw/hatebu-for-bing.user.js
// @updateURL https://gist.github.com/htsign/b2c913f28e4b8646d0b30c4170e7d34c/raw/hatebu-for-bing.user.js
// @grant none
// ==/UserScript==
(() => {
'use strict';
const CLASS = 'article-hatebu-count';
const ORIGIN = 'https://b.hatena.ne.jp';
const style = document.createElement('style');
document.head.append(style);
style.sheet.insertRule(`
.${CLASS} {
display: inline-block;
padding: 2px 4px;
line-height: 1rem;
}
`);
const range = document.createRange();
range.selectNode(document.documentElement);
const createHatebuImage = href => range.createContextualFragment(`
<a class="${CLASS}" href="${ORIGIN}/entry/${href}" target="_blank" rel="noopener">
<img src="${ORIGIN}/entry/image/${href}">
</a>
`);
const root = document.getElementById('b_results');
const iterate = nodes => {
const getUrl = li => {
const { href } = li.querySelector('h2 > a[href]');
return href;
};
for (const li of nodes.filter(node => node instanceof HTMLLIElement)) {
if (!li.classList.contains('b_algo')) continue;
const url = getUrl(li);
li.querySelector('.b_title').append(createHatebuImage(url));
}
};
const mo = new MutationObserver(records => {
iterate(records.flatMap(record => [...record.addedNodes]));
});
mo.observe(root, { childList: true });
iterate([...root.children]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment