Skip to content

Commit 6568042

Browse files
committed
fix
1 parent 87bac50 commit 6568042

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

scripts/backup/ext/pinterest-auto-save/content.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ let allBoardBtn,
77
const saveAllBtn = document.createElement("button");
88
saveAllBtn.innerText = "Save to All boards";
99
saveAllBtn.onclick = async () => {
10-
if (!isAutoClick) setCache("lastSaveIndex", 0);
10+
if (!isAutoClick) {
11+
const fromIndex = prompt("From index?", cached?.lastSaveIndex || 0);
12+
setCache("lastSaveIndex", parseInt(fromIndex) || 0);
13+
}
1114

1215
savingText.innerText =
1316
"Auto Saving to board " + ((cached?.lastSaveIndex || 0) + 1) + " ...";
@@ -69,14 +72,14 @@ saveAllBtn.onclick = async () => {
6972

7073
let done = false;
7174
while (!done) {
72-
const selector = `[data-test-id="${cur
73-
.getAttribute("data-test-id")
74-
.replaceAll('"', '\\"')}"]`;
75-
console.log("selector", selector);
76-
const nodes = await waitForElements(selector);
75+
const nodes = document.querySelectorAll(selector);
7776
const target = Array.from(nodes).find(
78-
(e) => cur != e && !beforeAllBoard.includes(e)
77+
(e) =>
78+
cur != e &&
79+
e.getAttribute("data-test-id") === cur.getAttribute("data-test-id") &&
80+
!beforeAllBoard.includes(e)
7981
);
82+
console.log(nodes, target);
8083
if (!target) {
8184
console.log("target not found, wait for load more...");
8285
await sleep(1000);
@@ -132,10 +135,13 @@ onElementsAdded(
132135

133136
const cacheKey = "pinterest-auto-save";
134137
function setCache(key, value) {
135-
const cached = JSON.parse(localStorage.getItem(cacheKey) || "{}");
136-
cached[key] = value;
137-
localStorage.setItem(cacheKey, JSON.stringify(cached));
138-
return cached;
138+
const _ = JSON.parse(localStorage.getItem(cacheKey) || "{}");
139+
_[key] = value;
140+
for (const k in _) {
141+
cached[k] = _[k];
142+
}
143+
localStorage.setItem(cacheKey, JSON.stringify(_));
144+
return _;
139145
}
140146

141147
function getCache(key) {
@@ -170,6 +176,14 @@ function focusTo(ele) {
170176
ele.scrollIntoView({ behavior: "smooth", block: "center" });
171177
}
172178

179+
function escapeCssSelector(str, deep = true) {
180+
if (!deep) return str.replaceAll('"', '\\"');
181+
return str
182+
.replace(/\\/g, "\\\\") // Escape backslashes first
183+
.replace(/([!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~])/g, "\\$1") // Escape other special characters
184+
.replace(/\s/g, "\\ "); // Escape spaces
185+
}
186+
173187
function sleep(ms) {
174188
return new Promise((resolve) => setTimeout(resolve, ms));
175189
}

0 commit comments

Comments
 (0)