Instagram为关注用户添加备注

为所关注的用户添加备注功能,以帮助识别

当前为 2019-07-25 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Instagram为关注用户添加备注
// @namespace    https://greasyfork.org/zh-CN/users/193133-pana
// @homepage     https://www.sailboatweb.com
// @version      1.0.0
// @description  为所关注的用户添加备注功能,以帮助识别
// @author       pana
// @include      http*://www.instagram.com/*
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
	'use strict';
	var user_handle = {
		user_id: '',
		user_tag: '',
	};
	var instagram_config = {};
	var default_config = {
		user_array: [],
	};
	const PAGE_REG = {
		HOMEPAGE: /^https?:\/\/www\.instagram\.com\/?(\?[a-z]+=[a-z\-]+)?$/i,
		USER_PAGE: /^https?:\/\/www\.instagram\.com\/[^/]*\/?(\?[a-z]+=[a-z\-]+)?$/i,
		STORIES: /^https?:\/\/www\.instagram\.com\/stories\/[^/]*\/?(\?[a-z]+=[a-z\-]+)?$/i,
	};

	function judge_User(user_title) {
		for (let i = 0; i < instagram_config.user_array.length; i++) {
			if (user_title === instagram_config.user_array[i].user_id) {
				return i
			}
		}
		return -1
	}
	function write_User(user_title, input_tag) {
		let judge_value = judge_User(user_title);
		if (judge_value !== -1) {
			if (input_tag) {
				instagram_config.user_array[judge_value].user_tag = input_tag
			} else {
				instagram_config.user_array.splice(judge_value, 1)
			}
		} else {
			if (input_tag) {
				let temp_user_obj = {
					user_id: user_title,
					user_tag: input_tag,
				};
				instagram_config.user_array.push(temp_user_obj)
			}
		}
		GM_setValue('instagram_config', instagram_config)
	}
	function create_Add_Input_Div(user_title) {
		let presentation_div = document.createElement('div');
		presentation_div.className = 'presentation_div_for_user';
		presentation_div.style.display = 'flex';
		presentation_div.style.position = 'fixed';
		presentation_div.style.backgroundColor = 'rgba(0, 0, 0, .5)';
		presentation_div.style.top = '0';
		presentation_div.style.bottom = '0';
		presentation_div.style.left = '0';
		presentation_div.style.right = '0';
		presentation_div.style.zIndex = '1';
		presentation_div.style.alignItems = 'center';
		presentation_div.style.justifyContent = 'center';
		presentation_div.addEventListener('click', function(event) {
			if (event.target === this) {
				$('.presentation_div_for_user').remove()
			}
		});
		let dialog_div = document.createElement('div');
		dialog_div.className = 'dialog_div_for_user';
		dialog_div.style.position = 'relative';
		dialog_div.style.width = '400px';
		dialog_div.style.backgroundColor = '#fff';
		dialog_div.style.border = '0 solid #000';
		dialog_div.style.borderRadius = '12px';
		let user_title_p = document.createElement('button');
		user_title_p.className = 'user_title_span_for_user';
		user_title_p.innerText = user_title;
		user_title_p.style.minHeight = '48px';
		user_title_p.style.textAlign = 'center';
		user_title_p.style.border = '1px solid #efefef';
		user_title_p.style.color = 'red';
		user_title_p.style.fontWeight = 'bold';
		user_title_p.style.backgroundColor = 'rgba(0, 0, 0, 0)';
		user_title_p.style.borderTopLeftRadius = '12px';
		user_title_p.style.borderTopRightRadius = '12px';
		let tag_input = document.createElement('input');
		tag_input.className = 'tag_input_for_user';
		tag_input.type = 'text';
		tag_input.placeholder = '(请输入备注,置空时删除)';
		tag_input.style.minHeight = '32px';
		tag_input.style.margin = '5px';
		let judge_value = judge_User(user_title);
		if (judge_value !== -1) {
			tag_input.value = instagram_config.user_array[judge_value].user_tag
		} else {
			tag_input.value = ''
		}
		let save_button = document.createElement('button');
		save_button.className = 'save_button_for_user';
		save_button.type = 'button';
		save_button.innerText = '保存';
		save_button.style.minHeight = '48px';
		save_button.style.cursor = 'pointer';
		save_button.style.border = '1px solid #efefef';
		save_button.style.backgroundColor = 'rgba(0, 0, 0, 0)';
		save_button.addEventListener('click', function() {
			write_User(user_title, $('.tag_input_for_user').val());
			save_Update_Event(user_title);
			$('.presentation_div_for_user').remove()
		});
		let cancel_button = document.createElement('button');
		cancel_button.className = 'cancel_button_for_user';
		cancel_button.type = 'button';
		cancel_button.innerText = '取消';
		cancel_button.style.minHeight = '48px';
		cancel_button.style.cursor = 'pointer';
		cancel_button.style.border = '1px solid #efefef';
		cancel_button.style.backgroundColor = 'rgba(0, 0, 0, 0)';
		cancel_button.style.borderBottomLeftRadius = '12px';
		cancel_button.style.borderBottomRightRadius = '12px';
		cancel_button.addEventListener('click', function(event) {
			$('.presentation_div_for_user').remove()
		});
		dialog_div.appendChild(user_title_p);
		dialog_div.appendChild(tag_input);
		dialog_div.appendChild(save_button);
		dialog_div.appendChild(cancel_button);
		presentation_div.appendChild(dialog_div);
		return presentation_div
	}
	function create_Add_Tags_A(text_string, user_title) {
		let tags_a = document.createElement('a');
		tags_a.className = 'Tags_A';
		tags_a.href = 'javascript:;';
		tags_a.title = text_string;
		tags_a.innerText = text_string;
		tags_a.style.marginLeft = '5px';
		tags_a.addEventListener('click', function() {
			document.body.appendChild(create_Add_Input_Div(user_title))
		});
		return tags_a
	}
	function create_Add_Tag_P(tag_string) {
		let tag_p = document.createElement('p');
		tag_p.className = 'tag_p';
		tag_p.style.marginLeft = '5px';
		tag_p.style.color = '#990033';
		tag_p.innerText = '(' + tag_string + ')';
		return tag_p
	}
	function create_Add_Tag_Span(tag_string, font_size) {
		let tag_span = document.createElement('span');
		tag_span.className = 'tag_span';
		tag_span.style.marginLeft = '5px';
		tag_span.style.color = '#990033';
		tag_span.style.fontSize = font_size;
		tag_span.innerText = '(' + tag_string + ')';
		return tag_span
	}
	function homepage_Event(dom_container) {
		let user_title = $(dom_container).find('a.nJAzx').attr('title');
		let judge_value = judge_User(user_title);
		if (judge_value !== -1) {
			$(dom_container).find('.e1e1d').append(create_Add_Tag_P(instagram_config.user_array[judge_value].user_tag))
		}
		$(dom_container).find('.e1e1d').append(create_Add_Tags_A('备注', user_title))
	}
	function homepage_Stories_Event(dom_container) {
		let user_title = $(dom_container).find('.jQgLo').text();
		let judge_value = judge_User(user_title);
		if (judge_value !== -1) {
			$(dom_container).find('.jQgLo').append(create_Add_Tag_Span(instagram_config.user_array[judge_value].user_tag, '12px'))
		}
	}
	function user_Page_Event(selector_container) {
		let user_title = selector_container.find('.KV-D4').text();
		selector_container.find('.AFWDX').after(create_Add_Tags_A('备注', user_title));
		let judge_value = judge_User(user_title);
		if (judge_value !== -1) {
			selector_container.find('.AFWDX').after(create_Add_Tag_Span(instagram_config.user_array[judge_value].user_tag, '16px'))
		}
		$.each(selector_container.find('span._32eiM'), function(index, item) {
			let em_user_title = item.innerText;
			let em_judge_value = judge_User(em_user_title);
			if (em_judge_value !== -1) {
				item.innerText = em_user_title + ' (' + instagram_config.user_array[em_judge_value].user_tag + ')'
			}
		})
	}
	function stories_Page_Event(selector_container) {
		let user_title = selector_container.find('.FPmhX').text();
		let judge_value = judge_User(user_title);
		if (judge_value !== -1) {
			selector_container.append(create_Add_Tag_Span(instagram_config.user_array[judge_value].user_tag, '14px'))
		}
	}
	function follow_Page_Event(selector_container) {
		if (selector_container.find('.tag_span').length === 0) {
			let user_title = selector_container.find('a.FPmhX').attr('title');
			let judge_value = judge_User(user_title);
			if (judge_value !== -1) {
				selector_container.find('a.FPmhX').parent().append(create_Add_Tag_Span(instagram_config.user_array[judge_value].user_tag, '14px'))
			}
		}
	}
	function save_Update_Event(user_title) {
		let old_url = location.href;
		if (PAGE_REG.HOMEPAGE.test(old_url)) {
			$.each($('article'), function(index, item) {
				let page_user_title = $(item).find('a.nJAzx').attr('title');
				if (user_title === page_user_title) {
					let judge_value = judge_User(user_title);
					if (judge_value !== -1) {
						if ($(item).find('p.tag_p').length !== 0) {
							$(item).find('p.tag_p').text('(' + instagram_config.user_array[judge_value].user_tag + ')')
						} else {
							$(item).find('.Tags_A').before(create_Add_Tag_P(instagram_config.user_array[judge_value].user_tag))
						}
					} else {
						if ($(item).find('p.tag_p').length !== 0) {
							$(item).find('p.tag_p').remove()
						}
					}
				}
			});
			$.each($('.BI5t6'), function(index, item) {
				let page_user_selector = $(item).find('.jQgLo').clone();
				page_user_selector.find('.tag_span').remove();
				let page_user_title = page_user_selector.text();
				if (user_title === page_user_title) {
					let judge_value = judge_User(user_title);
					if (judge_value !== -1) {
						if ($(item).find('span.tag_span').length !== 0) {
							$(item).find('span.tag_span').text('(' + instagram_config.user_array[judge_value].user_tag + ')')
						} else {
							$(item).find('.jQgLo').append(create_Add_Tag_Span(instagram_config.user_array[judge_value].user_tag, '12px'))
						}
					} else {
						if ($(item).find('span.tag_span').length !== 0) {
							$(item).find('span.tag_span').remove()
						}
					}
				}
			})
		} else if (PAGE_REG.USER_PAGE.test(old_url)) {
			let user_title = $('.KV-D4').text();
			let judge_value = judge_User(user_title);
			if (judge_value !== -1) {
				if ($('.tag_span').length !== 0) {
					$('.tag_span').text('(' + instagram_config.user_array[judge_value].user_tag + ')')
				} else {
					$('.AFWDX').after(create_Add_Tag_Span(instagram_config.user_array[judge_value].user_tag, '16px'))
				}
			} else {
				if ($('.tag_span').length !== 0) {
					$('.tag_span').remove()
				}
			}
		}
	}
	function init() {
		$.each($('article'), function(index, item) {
			homepage_Event(item)
		});
		$.each($('.BI5t6'), function(index, item) {
			homepage_Stories_Event(item)
		});
		$('#react-root').arrive('article', function() {
			homepage_Event(this)
		});
		$('#react-root').arrive('.BI5t6', function() {
			homepage_Stories_Event(this)
		});
		if ($('.zwlfE').length !== 0) {
			user_Page_Event($('.zwlfE'))
		}
		$('#react-root').arrive('.zwlfE', function() {
			user_Page_Event($(this))
		});
		if ($('.yn6BW').length !== 0) {
			stories_Page_Event($('.yn6BW'))
		}
		$('#react-root').arrive('.yn6BW', function() {
			stories_Page_Event($(this))
		});
		$('body').arrive('.isgrP li', {
			onceOnly: true
		}, function() {
			follow_Page_Event($(this))
		});
		$('body').arrive('.d7ByH', function() {
			follow_Page_Event($(this))
		})
	}
	Promise.all([GM_getValue('instagram_config')]).then(function(data) {
		if (data[0] !== undefined) {
			instagram_config = data[0]
		} else {
			instagram_config = default_config
		}
		init()
	})
})();