Skip to content

Instantly share code, notes, and snippets.

@umyuu
Last active June 29, 2024 00:36
Show Gist options
  • Save umyuu/e18638b5704e1799b04907f494c85afc to your computer and use it in GitHub Desktop.
Save umyuu/e18638b5704e1799b04907f494c85afc to your computer and use it in GitHub Desktop.
DeviantArt用のエンゲージメント率を算出用のブックマークレットです。

DeviantArt用のエンゲージメント率を算出用のブックマークレット

使用方法の詳しい内容は、noteに書いた記事を参考にしてください。
https://note.com/umyu/n/nefd70169e9f5

javascript:(() => {
    const targetUrl = 'https://www.deviantart.com/studio/published';
    if (location.href !== targetUrl) {
        location.href = targetUrl; /* 現在表示しているページがpublishedページではない場合、publishedのページに移動します。 */
        return;
    }
    
	function getContent() {
    	const searchKeyword = "Search";
		const pageText = document.documentElement.innerText;
		const searchIndex = pageText.indexOf(searchKeyword);

		if (searchIndex === -1) {
			/* "Search"が見つからなかった場合に例外を発生させる */
			throw new Error(`The keyword '${searchKeyword}' was not found in the page text.`);
		}
		return pageText.substring(searchIndex + searchKeyword.length);
    }

    function parseViewCount(viewString) {
        let multiplier = 1;
        if (viewString.endsWith('K')) {
            multiplier = 1000;
            viewString = viewString.slice(0, -1);
        } else if (viewString.endsWith('M')) {
            multiplier = 1000000;
            viewString = viewString.slice(0, -1);
        }
        return parseFloat(viewString) * multiplier;
    }
    function extractTagCount(tagString) {
        /* "Tags (3)" や "Tags (4)" のような文字列からタグ数を抽出 */
        const match = tagString.match(/Tags \((\d+)\)/);
        return match ? parseInt(match[1], 10) : 0;
    }
    function processLine(line) {
        /* 行を処理し、エンゲージメント率を計算して行を更新します */
        const columns = line.split('\t');
        const tags = extractTagCount(columns[2]);
        const likes = parseInt(columns[3], 10);
        const comments = parseInt(columns[4], 10);
        const views = parseViewCount(columns[5]);
        const engagementRate = (likes === 0) ? 0 : ((likes + comments) * 100 / views).toFixed(3);
        return columns[0] + '\t' + columns[1] + '\t' + tags + '\t' + likes + '\t' + comments + '\t' + columns[5] + '\t' + engagementRate + '%';
    }

    try {
		let pageText = getContent();
		/* テキストから文字列を削除 */
		pageText = pageText.replace(/Mature|Created with AI|NoAI|Favourites?|Comments?|Views?/g, '');
        
    	pageText = pageText.replace(/^\s*\n/gm, '');
    	/* 改行だけの行をタブに置き換えます */
    	pageText = pageText.replace(/\n/g, '\t');
    
    	pageText = pageText.replace(/Edit\t/g, '\n');
    
    	/* 各行のエンゲージメント率を計算して追加 */
    	let lines = pageText.split('\n');
    	let footer = lines.pop(); /* 最終行を削除 */
    	lines = lines.map(processLine);
    	pageText = lines.join('\n');
    
		const header = 'タイトル\t投稿日\tタグ数\tいいね数\tコメント数\t閲覧数\tエンゲージメント率\n';
		pageText = header + pageText + '\n';
    
    	function copyToClipboard(resultText) {
        	const dummy = document.createElement("textarea");
        	document.body.appendChild(dummy);
        	dummy.value = resultText;
        	dummy.select();
        	document.execCommand("copy");
        	document.body.removeChild(dummy);
    		
        	alert("エンゲージメント率をコピーしました。");
    	}

		copyToClipboard(pageText);
    } catch (error) {
		/* 例外を捕捉してメッセージボックスに表示します */
        alert(error.message);
    }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment