// ==UserScript==
// @name Twitter(X) - Keep the search result
// @name:zh-TW 推特(X) - 儲存搜尋結果
// @name:zh-CN 推特(X) - 储存搜寻结果
// @name:ja ツイッター(X) ー 検索結果を保存
// @namespace http://tampermonkey.net/hello
// @version 1.02
// @description Just like old feature of twitter. Keep the search result by clicking icon in result page.
// @description:zh-TW 像是(那消失的)原生「已儲存的搜尋」功能,在搜尋結果頁點擊儲存,會保存輸入框內的搜尋結果。
// @description:zh-CN 像是(那消失的)原生「已储存的搜寻」功能,在搜寻结果页点击储存,会保存输入框内的搜寻结果。
// @description:ja (あの消えてしまった)元の「保存済み検索」機能と同じで、検索結果ページで保存すると、入力ボックス内の検索キーワードを保存します。
// @author Aray-not-Array
// @match https://x.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant none
// @license MIT
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
// config
const debug = false;
const searchurl = "https://x.com/search?q=";
const className = "save-search-by-Aray";
// config end
debug && console.log("SMSR Load success");
var input, searchBar, searchParent, fontColor, mode;
var values = [];
function checkAndAddButton() {
//search or hashtag page
if (!location.href.includes(searchurl) && !location.href.includes("https://x.com/hashtag") || document.querySelector(`.${className}`)) return;
input = document.querySelector('[data-testid="SearchBox_Search_Input"]') ||
document.querySelector('input[placeholder="Search"]') ||
document.querySelector('input[aria-label="Search"]');
searchBar = document.querySelector('[role="search"]');
debug && console.log(input);
debug && console.log(searchBar);
if (!searchBar) return;
//check theme
getCookie("night_mode=") != mode && SetColor();
searchParent = searchBar.parentElement.parentElement.parentElement.parentElement.parentElement;
const save = document.createElement('button');
save.className = className;
save.style.cssText = `
display:flex;
height:100%;
position:relative;
cursor: pointer;
border-color: rgba(0,0,0,0);
font-size: 24px;
background-color: rgba(0,0,0,0);
border-radius: 100%;
user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
transition: all 0.1s ease;
`;
save.innerHTML = `
`;
searchParent.insertAdjacentElement('afterend', save);
save.addEventListener('click', function () {
if (values.length > 0 && values.includes(input.value) || input.value == "") return;
debug && console.log(input.value);
values.push(input.value);
localStorage.setItem(`${className}-${user}`, JSON.stringify(values));
debug && console.log(values);
})
}
function checkAndAddList() {
//check theme
getCookie("night_mode=") != mode && SetColor();
//saved search list
if (values.length == 0 || document.querySelector(".AR-RecentSearchesItem")) return;
var container = document.querySelector('[data-testid="typeaheadEmptySearch"]') ||
document.querySelector('[data-testid="typeaheadRecentSearchesHeader"]')
debug && console.log(container);
if (!container) return;
values.forEach(function (v, i) {
const recent = document.createElement('div');
recent.href = `/search?q=${v}`;
recent.className = "AR-RecentSearchesItem";
recent.style.cssText = `
display:flex;
cursor: pointer;
border-color: rgba(0,0,0,0);
font-size: 16px;
background-color: rgba(0,0,0,0);
user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
color:${fontColor};
transition: all 0.1s ease;
`;
recent.innerHTML = `
`;
container.parentElement.insertAdjacentElement('beforeend', recent);
recent.addEventListener('click', (e) => {
if (e.target.closest(`#AR-delete-${i}`)) return;
location.href = `/search?q=${encodeURIComponent(v)}`;
});
const btn = document.getElementById(`AR-delete-${i}`);
btn.addEventListener('click', function () {
values.splice(i, 1);
localStorage.setItem(`${className}-${user}`, JSON.stringify(values));
recent.remove();
});
})
}
function getCookie(name) {
var cookie = document.cookie.split(name);
if (cookie.length == 2) {
return cookie = cookie[1].split(";", 2)[0];
}
return null;
}
//check url change
setInterval(checkAndAddButton, 1000);
setInterval(checkAndAddList, 1000);
//set dark/light mode
function SetColor() {
mode = getCookie("night_mode=");
fontColor = mode == 0 ? "#000" : "#FFF";
}
//first check
SetColor();
checkAndAddButton();
checkAndAddList();
//set saved result
const user = getCookie("; twid=u%3D");
if (localStorage.getItem(`${className}-${user}`)) values = JSON.parse(localStorage.getItem(`${className}-${user}`));
debug && console.log(values);
const style = document.createElement('style');
style.textContent = `
.save-search-by-Aray:hover,
.AR-RecentSearchesItem:hover{
background-color: rgb(255,255,255,0.05)!important;
}
.AR-delete-button:hover{
background-color: rgb(244,33,46,0.1);
}
`;
document.head.appendChild(style);
})();