PageSourceSearch

https://cf.mora.jp/cfdocs/pc/js/sns-widget2025.js?ver=20260918131340

js mora.jp collected 2026-09-24 09:03:59 UTC 3,084 bytes, 77 lines download raw bytes

1// 全角・半角を考慮して文字列をバイト数で切り詰める関数
2const truncateTextByByte = (text, maxLength = 108) => {
3	let currentByteLength = 0;
4	let result = "";
5	for (let i = 0; i < text.length; i++) {
6		const char = text.charAt(i);
7		const byteLength = char.charCodeAt(0) >= 0x00 && char.charCodeAt(0) <= 0x7f ? 1 : 2;
8		if (currentByteLength + byteLength > maxLength * 2) {
9			return result + "…";
10		}
11		currentByteLength += byteLength;
12		result += char;
13	}
14	return result;
15};
16
17window.addEventListener("load", function () {
18	// 外部ファイル(json等)読み込み後発火する
19	const shareUrl = location.href;
20	const pageTitle = document.title;
21	const shareText = truncateTextByByte(pageTitle, 108);
22
23	// Xボタン挿入
24	const tw = document.getElementById("snsBtn_tw");
25	if (tw) {
26		const a = document.createElement("a");
27		a.className = "twitter-share-button";
28		a.href = "https://twitter.com/share";
29		a.setAttribute("data-url", shareUrl);
30		a.setAttribute("data-text", shareText);
31		a.setAttribute("data-hashtags", "morajp");
32		a.setAttribute("data-lang", "ja");
33		a.setAttribute("data-show-count", "false");
34		a.innerText = "ポスト";
35		tw.appendChild(a);
36
37		if (window.twttr && window.twttr.widgets) {
38			window.twttr.widgets.load(tw);
39		}
40	}
41
42	// Facebookボタン(シェアボタン)を挿入
43	const fbElement = document.getElementById("snsBtn_fb");
44	if (fbElement) {
45		// 既存のコンテンツをクリア
46		// これにより、もし古い<iframe>が残っていても削除されます。
47		fbElement.innerHTML = "";
48
49		// シェアボタンのコンテナとなる<div>要素を生成
50		const shareButtonDiv = document.createElement("div");
51		// ★重要: Facebook SDKがシェアボタンとして認識するクラス
52		shareButtonDiv.classList.add("fb-share-button");
53
54		// シェア対象のURLを設定
55		// `shareUrl` は、この関数が呼び出されるスコープで定義されていると仮定します。
56		// 例: const shareUrl = location.href;
57		shareButtonDiv.setAttribute("data-href", shareUrl);
58
59		// レイアウト: シェア数なしのシンプルなボタン
60		shareButtonDiv.setAttribute("data-layout", "button");
61		// サイズ
62		shareButtonDiv.setAttribute("data-size", "small");
63
64		// 生成したシェアボタンの<div>要素をページに追加
65		fbElement.appendChild(shareButtonDiv);
66
67		// Facebook SDKがロード済みであれば、動的に追加されたボタンをパースしてレンダリングする
68		// これにより、JavaScriptで追加されたボタンもFacebookウィジェットとして機能します。
69		if (typeof FB !== "undefined" && FB.XFBML) {
70			FB.XFBML.parse(fbElement); // fbElement内の新しいXFBMLタグをパースする
71		} else {
72			// SDKがまだロードされていない、または利用できない場合の警告
73			// async/deferでSDKを読み込んでいる場合、通常はこの警告は出ないはずです。
74			console.warn("Facebook SDK not yet loaded or FB.XFBML is unavailable. Share button may render late.");
75		}
76	}
77});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.