自定义脚本分享:随机 Memos 回顾 #974
Replies: 8 comments 8 replies
-
随机条数可以增加吗?3-5条? |
Beta Was this translation helpful? Give feedback.
-
修改了一下,自动根据 RSS 链接获取用户 ID
|
Beta Was this translation helpful? Give feedback.
-
//随机挑转一条 Memo
function randomMemo() {
var creatorId = document.querySelector('a[href$="rss.xml"]').href.split('/')[4];
var statsURL = window.location.origin + "/api/memo/stats?creatorId=" + creatorId;
// fetch stats, and get the total number of memos,
// then get a random number between 0 and the total number of memos
// then get the memo at the random index
fetch(statsURL).then(function(response) {
return response.json();
}).then(function(json) {
var memosAmount = json.data.length;
var randomMemoIndex = Math.floor(Math.random() * memosAmount);
var randomMemoID = window.location.origin+"/api/memo?rowStatus=NORMAL&limit=1&offset="+randomMemoIndex;
fetch(randomMemoID).then(res2 => res2.json()).then( resdata2 =>{
var randomMemoURL = window.location.origin+"/m/"+resdata2.data[0].id;
// Open the random memo in a new tab
window.open(randomMemoURL, '_blank');
});
});
}
//插入随机按钮
setTimeout(function() {
document.querySelector(".sidebar-wrapper>.px-2>button").insertAdjacentHTML('afterend', '<button onclick="randomMemo()" class="leading-10 px-4 rounded-lg text-base dark:text-gray-200 hover:bg-white hover:shadow dark:hover:bg-zinc-700"><span class="mr-1">⛳️</span> 随机</button>');
}, 1500) 于v0.11.0版本测试通过。 |
Beta Was this translation helpful? Give feedback.
-
0.16.1单用户测试通过,写的比较糙 //随机跳转一条 Memo
function randomMemo() {
var creatorId = "123"; //修改为自己的用户 id
var statsURL = window.location.origin + "/api/v1/memo/stats?creatorUsername=" + creatorId;
// fetch stats, and get the total number of memos,
// then get a random number between 0 and the total number of memos
// then get the memo at the random index
fetch(statsURL).then(function(response) {
return response.json();
}).then(function(json) {
var memosAmount = json.length;
var randomMemoIndex = Math.floor(Math.random() * memosAmount);
var randomMemoURL = window.location.origin+"/m/"+randomMemoIndex;
// Open the random memo in a new tab
setTimeout(() => {
window.open(randomMemoURL, '_blank');
})
});
}
//插入随机按钮
setTimeout(function() {
document.getElementById("header-setting").insertAdjacentHTML('afterend', '<button onclick="randomMemo()" class="false px-4 pr-5 py-2 rounded-lg flex flex-row items-center text-lg text-gray-800 dark:text-gray-300 hover:bg-white hover:shadow dark:hover:bg-zinc-700"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="mr-3 w-6 h-auto opacity-70"><path d="M20 7h-9"></path><path d="M14 17H5"></path><circle cx="17" cy="17" r="3"></circle><circle cx="7" cy="7" r="3"></circle></svg> 随机回顾</div>');
}, 1500) |
Beta Was this translation helpful? Give feedback.
-
Version: v0.19.0更新了id,我根据楼上的代码改的,可能不够简洁,欢迎指正。
|
Beta Was this translation helpful? Give feedback.
-
Here's an updated version of this Memos Additional script, for As I tweaked the button style a little bit to better fit the current UI style. The only thing that's not pleasing me is the full page reload triggered by the button click. Edit history
CodeCheck this gist for the additional script code. |
Beta Was this translation helpful? Give feedback.
-
发现是我自己的问题,没有升级成功,注意这个脚本仅在0.19版本适用 --------- 分割线 最新更新 ---------
...
for (let i = 0; i < json.memos.length; i++) {
- const memeoUuid = json.memos[i].name;
+ const memoUuid = json.memos[i].id;
memoUuids.push(memoUuid);
}
...
完整代码: async function cacheMemosUuids() {
const memoAmount = 100;
const memoKind = ["PUBLIC", "PROTECTED"];
const memoCacheTimeMinutes = 60;
const memoCreatorUsername = "";
const storedmemoUuidCache = localStorage.getItem("randomMemoUuidCache");
const lastUpdate = localStorage.getItem("randomMemoUuidCacheLastUpdate");
const cacheTimeMillis = memoCacheTimeMinutes * 60 * 1000;
const mustUpdate =
lastUpdate === null || Date.now() - lastUpdate > cacheTimeMillis;
if (storedmemoUuidCache !== null && !mustUpdate) {
return;
}
let apiEndpoint =
window.location.origin +
"/api/v2/memos" +
`?pageSize=${memoAmount}` +
"&filter=" +
encodeURIComponent("visibilities==" + JSON.stringify(memoKind));
if (memoCreatorUsername) {
apiEndpoint += encodeURIComponent(
`&&creator=="users/${memoCreatorUsername}"`
);
}
await fetch(apiEndpoint)
.then(function (response) {
return response.json();
})
.then(function (json) {
if (json.memos.length === 0) {
return;
}
let memoUuids = [];
for (let i = 0; i < json.memos.length; i++) {
const memoUuid = json.memos[i].id;
memoUuids.push(memoUuid);
}
localStorage.setItem("randomMemoUuidCache", JSON.stringify(memoUuids));
localStorage.setItem("randomMemoUuidCacheLastUpdate", Date.now());
})
.catch(function (error) {
console.error(error);
});
}
async function updateRandomMemoLink() {
const storedUuids = localStorage.getItem("randomMemoUuidCache");
if (storedUuids === null) {
cacheMemosUuids();
return null;
}
const memoUuidCache = JSON.parse(storedUuids);
const randomIndex = Math.floor(Math.random() * memoUuidCache.length);
const randomMemoUuid = memoUuidCache[randomIndex];
const randomMemoButton = document.getElementById("header-random");
randomMemoButton.href = "/m/" + randomMemoUuid;
}
async function insertRandomMemoButton() {
if (document.getElementById("header-random") !== null) {
return;
}
let insertionTries = 0;
while (
document.getElementById("header-setting") === null &&
insertionTries < 20
) {
await new Promise((r) => setTimeout(r, 100));
insertionTries++;
}
const headerSetting = document.getElementById("header-setting");
if (headerSetting === null) {
return;
}
headerSetting.insertAdjacentHTML(
"afterend",
`
<a id="header-random"
class="px-2 py-2 rounded-2xl border flex flex-row items-center text-lg text-gray-800 dark:text-gray-300 hover:bg-white hover:border-gray-200 dark:hover:border-zinc-700 dark:hover:bg-zinc-800 w-full px-4 border-transparent"
href="/" title="Show a random memo">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-dices"><rect width="12" height="12" x="2" y="10" rx="2" ry="2"/><path d="m17.92 14 3.5-3.5a2.24 2.24 0 0 0 0-3l-5-4.92a2.24 2.24 0 0 0-3 0L10 6"/><path d="M6 18h.01"/><path d="M10 14h.01"/><path d="M15 6h.01"/><path d="M18 9h.01"/></svg>
<span class="ml-3">随机回顾</span>
</a>`
);
}
(async () => {
try {
await cacheMemosUuids();
await insertRandomMemoButton();
await updateRandomMemoLink();
document.getElementById("header-random").onclick = async () => {
await updateRandomMemoLink();
};
document.getElementById("header-random").onauxclick = async () => {
await updateRandomMemoLink();
};
} catch (e) {
console.error(e);
}
})(); |
Beta Was this translation helpful? Give feedback.
-
目前的版本是v0.22.4,我提供一个在前面代码基础上修改过的可以使用的版本。主要改动有二
The current version is v0.22.4. I am providing a modified version based on the previous code. The main changes are as follows:
async function cacheMemosUuids() {
const memoAmount = 1000;
const memoKind = ["PUBLIC", "PRIVATE"];
const memoCacheTimeMinutes = 60;
const memoCreatorUsername = "";
const storedmemoUuidCache = localStorage.getItem("randomMemoUuidCache");
const lastUpdate = localStorage.getItem("randomMemoUuidCacheLastUpdate");
const cacheTimeMillis = memoCacheTimeMinutes * 60 * 1000;
const mustUpdate =
lastUpdate === null || Date.now() - lastUpdate > cacheTimeMillis;
if (storedmemoUuidCache !== null && !mustUpdate) {
return;
}
let apiEndpoint =
window.location.origin +
"/api/v1/memos" +
`?pageSize=${memoAmount}` +
"&filter=" +
encodeURIComponent("visibilities==" + JSON.stringify(memoKind));
if (memoCreatorUsername) {
apiEndpoint += encodeURIComponent(
`&&creator=="users/${memoCreatorUsername}"`
);
}
await fetch(apiEndpoint)
.then(function (response) {
return response.json();
})
.then(function (json) {
console.log(json);
if (json.memos.length === 0) {
return;
}
let memoUuids = [];
for (let i = 0; i < json.memos.length; i++) {
const memoUuid = json.memos[i].uid;
memoUuids.push(memoUuid);
}
localStorage.setItem("randomMemoUuidCache", JSON.stringify(memoUuids));
localStorage.setItem("randomMemoUuidCacheLastUpdate", Date.now());
})
.catch(function (error) {
console.error(error);
});
}
async function updateRandomMemoLink() {
const storedUuids = localStorage.getItem("randomMemoUuidCache");
if (storedUuids === null) {
cacheMemosUuids();
return null;
}
const memoUuidCache = JSON.parse(storedUuids);
const randomIndex = Math.floor(Math.random() * memoUuidCache.length);
const randomMemoUuid = memoUuidCache[randomIndex];
const randomMemoButton = document.getElementById("header-random");
randomMemoButton.href = "/m/" + randomMemoUuid;
}
async function insertRandomMemoButton() {
if (document.getElementById("header-random") !== null) {
return;
}
let insertionTries = 0;
while (
document.getElementById("header-setting") === null &&
insertionTries < 20
) {
await new Promise((r) => setTimeout(r, 100));
insertionTries++;
}
const headerSetting = document.getElementById("header-setting");
if (headerSetting === null) {
return;
}
headerSetting.insertAdjacentHTML(
"afterend",
`
<a id="header-random"
class="px-2 py-2 rounded-2xl border flex flex-row items-center text-lg text-gray-800 dark:text-gray-300 hover:bg-white hover:border-gray-200 dark:hover:border-zinc-700 dark:hover:bg-zinc-800 w-full px-4 border-transparent"
href="/" title="Show a random memo">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-dices"><rect width="12" height="12" x="2" y="10" rx="2" ry="2"/><path d="m17.92 14 3.5-3.5a2.24 2.24 0 0 0 0-3l-5-4.92a2.24 2.24 0 0 0-3 0L10 6"/><path d="M6 18h.01"/><path d="M10 14h.01"/><path d="M15 6h.01"/><path d="M18 9h.01"/></svg>
<span class="ml-3">随机回顾</span>
</a>`
);
}
(async () => {
try {
await cacheMemosUuids();
await insertRandomMemoButton();
await updateRandomMemoLink();
document.getElementById("header-random").onclick = async () => {
await updateRandomMemoLink();
};
document.getElementById("header-random").onauxclick = async () => {
await updateRandomMemoLink();
};
} catch (e) {
console.error(e);
}
})(); |
Beta Was this translation helpful? Give feedback.
-
基于 v0.12.2
以下,归档。
添加到:设置--系统--自定义脚本
Beta Was this translation helpful? Give feedback.
All reactions