Skip to content

Commit

Permalink
Refactor code and remove unnecessary scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
tztsai committed Nov 12, 2024
1 parent 76c970d commit 6de4988
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
6 changes: 1 addition & 5 deletions background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ importScripts('/background/icon.js')

var mdWise = (tab) => {
inject(tab.id);
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["/content/mdwise.js", "/content/index.js"]
})
}

chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "md-wise",
title: "Too Long Read It Short",
title: "Assist Me To Read",
contexts: ["page"]
});
});
Expand Down
4 changes: 1 addition & 3 deletions background/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ md.inject = ({ storage: { state } }) => (id) => {
pre = document.querySelector('pre');
if (pre) {
pre.style.visibility = 'hidden'
return
} else {
console.warn('No <pre> element found');
}
},
injectImmediately: true
Expand All @@ -44,6 +41,7 @@ md.inject = ({ storage: { state } }) => (id) => {
state.content.emoji && '/content/emoji.js',
state.content.mermaid && ['/vendor/mermaid.min.js', '/vendor/panzoom.min.js', '/content/mermaid.js'],
state.content.mathjax && ['/content/mathjax.js', '/vendor/mathjax/tex-mml-chtml.js'],
'/content/mdwise.js',
'/content/index.js',
'/content/scroll.js',
state.content.autoreload && '/content/autoreload.js',
Expand Down
2 changes: 0 additions & 2 deletions background/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ md.messages = ({ storage: { defaults, state, set }, compilers, mathjax, xhr, web
// files: ['/vendor/readability.min.js', '/vendor/turndown.min.js'],
func: () => {
try {
document.readyState = 'loading';
// let content = new Readability(document).parse().content;
let content = document.body;
content = new TurndownService().turndown(content);
document.body.innerHTML = `<pre>${content}</pre>`;
document.readyState = 'complete';
return { content };
} catch (err) {
console.error('Readability error:', err);
Expand Down
15 changes: 7 additions & 8 deletions content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function mount() {
$('pre').style.display = 'none'
var md = $('pre').innerText
} catch (e) {
console.error('No <pre> element found')
state.html = $('body').innerHTML
var md = ''
}
Expand Down Expand Up @@ -298,17 +299,15 @@ if (!document.querySelector('pre')) {
// clean the HTML content
cleanHtml();
// convert the HTML content to markdown
chrome.runtime.sendMessage({ message: 'to-markdown' });
}

if (document.readyState === 'complete') {
mount()
}
else {
chrome.runtime.sendMessage(
{ message: 'to-markdown' },
(_) => mount()
);
} else {
var timeout = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(timeout)
mount()
}
}, 0)
})
}
10 changes: 8 additions & 2 deletions content/mdwise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
function cleanHtml() {
window.document.querySelectorAll(
'link, style, script, meta, noscript, header, nav, span, footer, div[role="navigation"], figure, table'
if (articles = document.querySelectorAll('article')) {
document.body.innerHTML = '';
articles.forEach(e => document.body.appendChild(e));
} else if (main = document.querySelector('main')) {
document.body.innerHTML = main.innerHTML;
}
document.querySelectorAll(
'link, style, script, meta, noscript, header, nav, span, footer, figure, table'
).forEach(e => e.remove());
}

Expand Down

0 comments on commit 6de4988

Please sign in to comment.