-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
151 lines (125 loc) · 3.93 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import fetch from "node-fetch";
interface MyPluginSettings {
autoArchiveOnDocumentLoad: boolean;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
autoArchiveOnDocumentLoad: false
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
console.log("Loading Link Archiver");
await this.loadSettings();
// this.addRibbonIcon('dice', 'Sample Plugin', () => {
// new Notice('This is a notice!');
// });
this.addCommand({
id: 'auto-link-archiver-save-open-page',
name: 'Archive all links on current page',
checkCallback: (checking: boolean) => {
let leaf = this.app.workspace.activeLeaf;
if (leaf) {
if (!checking) {
new SampleModal(this.app, this.cm).open();
}
return true;
}
return false;
}
});
// this.addSettingTab(new SampleSettingTab(this.app, this));
this.registerCodeMirror((cm: CodeMirror.Editor) => {
console.log('codemirror', cm);
this.cm = cm;
cm.on("cursorActivity", (cm: CodeMirror.Editor) => {
const current = cm.getCursor().line + 1;
if (cm.state.curLineNum === current) {
return;
}
// console.log(current);
// console.log("doc", cm.getDoc().getValue());
});
this.registerEvent(
this.app.metadataCache.on("changed", (file) => {
// file saves, etc.
console.log('metadata cache changed', file);
})
);
});
// const a = document.getElementsByClassName("CodeMirror");
// console.log("a", a);
}
onunload() {
console.log('unloading Link Archiver plugin');
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SampleModal extends Modal {
constructor(app: App, cm: CodeMirror.Editor) {
super(app);
this.cm = cm;
this.modalContent = "";
}
onOpen() {
let {contentEl} = this;
let urlStatuses = [];
const currentNoteContents = this.cm.getDoc().getValue();
const regex = /(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/g
const urlMatches = currentNoteContents.match(regex) || [];
const matchCount = urlMatches.length;
if (matchCount === 0){
return;
}
const header = (index) => `<h1>Link Archiving Status (${index}/${matchCount})</h1>`
let i = 0;
contentEl.innerHTML = header(i);
urlMatches.forEach(async (url) => {
const result = await fetch(`https://web.archive.org/save/${url}`)
const status = {
sourceUrl: url,
url: result.url,
status: result.status,
ok: result.ok
}
urlStatuses.push(status);
contentEl.innerHTML = `
${header(++i)}
<ul>
${urlStatuses.map(result => `<li>${result.ok ? '✓' : '✖' } <a href="${result.sourceUrl}">${result.sourceUrl}</a> => <a href="${result.url}">${result.url}</a></li>`).join('')}
</ul>
`
});
}
onClose() {
let {contentEl} = this;
contentEl.empty();
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for Link Archiver'});
containerEl.createEl('h4', {text: 'Currently None :)'});
// new Setting(containerEl)
// .setName('Auto Archive on document open')
// .setDesc('Auto archive links anytime you open a document. We\'d rather not overload our friends at archive.org (They run the WaybackMachine).')
// .addToggle(toggle => toggle
// .onChange(async (value) => {
// console.log('Secret: ' + value);
// this.plugin.settings.autoArchiveOnDocumentLoad = value;
// await this.plugin.saveSettings();
// }));
}
}