-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
92 lines (84 loc) · 3.19 KB
/
background.js
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
/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
// original: https://gist.github.com/piroor/14e42c7eeb876ab71e3202dabcd0ee9e#file-toc-js
function generate() {
var container = document.querySelector('.markdown-body');
var tab = document.querySelector('.preview-tab');
tab.click();
setTimeout(function waitPreview(retryCount) {
container = document.querySelector('.preview-content .markdown-body');
if (!container || (retryCount < 10 && !generateToC()))
setTimeout(waitPreview, 250, retryCount + 1);
}, 250, 0);
function generateToC() {
var headings = container.querySelectorAll('h1, h2, h3, h4, h5, h6');
if (headings.length == 0)
return false;
var minLevel = 6;
for (let heading of headings) {
let headingLevel = parseInt(heading.localName.charAt(1));
if (headingLevel < minLevel)
minLevel = headingLevel;
}
var toc = [];
for (let heading of headings) {
let anchor = heading.querySelector('a.anchor[href]') || heading.parentNode.querySelector('a.anchor[href]');
if (!anchor)
throw new Error(`Could not find out anchor for a heading: ${heading.textContent}`);
let href = anchor.getAttribute('href');
let indent = '';
let headingLevel = parseInt(heading.localName.charAt(1));
for (let level = headingLevel - minLevel; level > 0; level--) {
indent += ' ';
}
toc.push(`${indent}* [${heading.textContent.trim().replace(/\s\s+/g, ' ')}](${href})`);
}
var source = `<!--ToC-->
${toc.join('\n')}
<small>(generated by [Table of Contents Generator for GitHub Wiki](https://addons.mozilla.org/firefox/addon/toc-generator-for-github-wiki/))</small>
<!--/ToC-->`;
let tab = document.querySelector('.write-tab');
tab.click();
setTimeout(function updateToC(retryCount) {
var field = document.querySelector('#gollum-editor-body');
if (retryCount < 10 && !field.value)
return setTimeout(updateToC, 250, retryCount);
var matcher = /<!--ToC-->\n(.*\n)*<!--\/ToC-->/;
if (matcher.test(field.value))
field.value = field.value.replace(matcher, source);
else
field.value = `${source}
${field.value}`;
}, 250, 0);
}
}
browser.browserAction.onClicked.addListener(async (aTab) => {
var baseURL = aTab.url.split('#')[0].split('?')[0];
var isWikiPage = /https:\/\/github.com\/[^\/]+\/[^\/]+\/wiki($|\/)/.test(baseURL);
if (!isWikiPage)
return;
var isEditPage = /\/_edit$/.test(baseURL);
if (!isEditPage) {
var waitUntilLoaded = new Promise((aResolve, aReject) => {
let onUpdated = (aTabId, aChangeInfo, aCompleteTab) => {
if (aTabId != aTab.id ||
aChangeInfo.status != 'complete')
return;
browser.tabs.onUpdated.removeListener(onUpdated);
aResolve();
};
browser.tabs.onUpdated.addListener(onUpdated);
});
await browser.tabs.update(aTab.id, {
url: baseURL += '/_edit'
});
await waitUntilLoaded;
}
browser.tabs.executeScript(aTab.id, {
code: `(${generate.toString()})()`
});
});