Skip to content

Commit 9fac871

Browse files
committed
update(html editor): notif - detect failed update
1 parent 9807a2d commit 9fac871

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

background.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,56 @@
1+
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest,
2+
{ urls: ["https://draft.blogger.com/_/BloggerUi/data/*"], types: ["xmlhttprequest"] }
3+
);
4+
15
chrome.webRequest.onCompleted.addListener(onRequestCompleted,
26
{ urls: ["https://draft.blogger.com/_/BloggerUi/data/*"], types: ["xmlhttprequest"] }
37
);
48

5-
function onRequestCompleted(details) {
9+
let isNotifiedForErrors = false;
10+
11+
function onBeforeRequest() {
12+
isNotifiedForErrors = false;
13+
}
14+
15+
function waitUntil(stateCheckCallback, delay = 100) {
16+
return new Promise(resolve => {
17+
let interval = setInterval(() => {
18+
let shouldResolve = stateCheckCallback();
19+
if (shouldResolve) {
20+
clearInterval(interval);
21+
resolve();
22+
}
23+
}, delay);
24+
});
25+
}
26+
27+
let waitTimeout;
28+
29+
async function onRequestCompleted(details) {
630

731
if (!details.url.includes('rpcids=ZmRkFc')) return;
8-
32+
33+
waitTimeout = setTimeout(() => {
34+
isNotifiedForErrors = true;
35+
sendNotif('Check for errors');
36+
}, 1000);
37+
}
38+
39+
function sendNotif(message) {
40+
941
let notifId = '1';
1042
const notificationOptions = {
1143
type: 'basic',
1244
iconUrl: "data:image/webp;base64,UklGRiIBAABXRUJQVlA4TBUBAAAvH8AHAH/AJgDAJNMo6/8vjbsb8FHDSCQp675/ETKhUCgKoCaSFGlsIB5JaCCnyN7Lbv4D4P/flT5lC5QSUOHABniGRfBybZ80y3YB3GrblidVBviti2eYtPrEPQE2YIPICqwQrdA3UlH9tFRU7u5u3/viA0T0fwK0f9Jz/pVzzfO59ZfTkxXnJ9bzKBRgO6URWFtYz3CzTm50XQBwXYDNrGfGjwDGj1jmCOKsUxnCEQbgCANSSAkBa0ywDHU6EOeaU6g1gfpMfJWpx2FpwMQUmBGAWhPqJ8RtZfyVc2SkWaXQIoEIbk17fV0iZZbaD2m3pmlaf4nUdsv9ypy3WkTUbh1o4mu/1Wq1DtySdv56enLi1v5HAA==",
13-
title: `Done. Refresh blog page or check errors.`,
14-
message: ``,
45+
title: ``,
46+
message,
1547
requireInteraction: true,
1648
};
1749
chrome.notifications.create(notifId, notificationOptions);
1850

19-
// execInPage('console.log(123)');
20-
21-
2251
setTimeout(function() {
2352
chrome.notifications.clear(notifId);
2453
}, 2000);
25-
2654
}
2755

2856

@@ -47,10 +75,11 @@ async function execInPage(code) {
4775
// background.js
4876
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
4977
if (request.action === 'sendMessageFromBackground') {
50-
console.log('Message received in background:', request.data);
51-
52-
// Send a response back to the content script
53-
// sendResponse({ response: 'Response from background' });
78+
if (request.data == 200) {
79+
clearInterval(waitTimeout);
80+
if (!isNotifiedForErrors) {
81+
sendNotif('Template updated');
82+
}
83+
}
5484
}
55-
});
56-
85+
});

inject-all-pages.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,33 @@ if (targetElement) {
2020
observer.observe(targetElement, config);
2121
}
2222

23+
let init = false;
24+
25+
async function applyUpdateWatcher() {
26+
27+
await waitUntil(() => $('[aria-live="polite"]'), 200);
28+
29+
const observer = new MutationObserver( (mutationsList, observer) => {
30+
for (const mutation of mutationsList) {
31+
32+
if (mutation.target.textContent.trim() == 'Update successful.') {
33+
chrome.runtime.sendMessage({
34+
action: 'sendMessageFromBackground',
35+
data: 200,
36+
});
37+
}
38+
39+
}
40+
});
41+
42+
const config = {
43+
childList: true,
44+
};
45+
observer.observe($('[aria-live="polite"]'), config);
46+
}
47+
2348
applyMutation();
49+
applyUpdateWatcher();
2450

2551
function waitUntil(stateCheckCallback, delay = 100) {
2652
return new Promise(resolve => {

post-editor-util.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ let utilPostEditor = (function() {
4040

4141
// set format block
4242
editorEl.contentDocument.documentElement.addEventListener('keydown', function(event) {
43-
if (event.ctrlKey && event.key === '0') {
43+
if (event.ctrlKey && event.key === '5') {
4444
event.preventDefault();
4545
editorEl.contentDocument.execCommand('formatBlock', false, 'p');
4646
} else if (event.ctrlKey && event.code === 'Space') {
4747
event.preventDefault();
4848
$('[aria-label="Clear formatting"]').click();
49+
} else if (event.ctrlKey && event.key === 's') {
50+
event.preventDefault();
51+
$('[aria-label="Update"]').click();
4952
} else if (event.ctrlKey && event.key === 'm') {
5053
event.preventDefault();
5154
document.querySelector('iframe.editable').contentDocument.execCommand('backColor', false, '#fcff01');

0 commit comments

Comments
 (0)