-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
79 lines (77 loc) · 2.57 KB
/
menu.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
/* eslint no-console: "off" */
menu_id = browser.contextMenus.create({
"id": "ptw",
"title": "Add to MAL PTW list",
"contexts": ["link"]
});
browser.contextMenus.onClicked.addListener((info, tab) => {
console.log("hello");
if (info.menuItemId === menu_id) {
console.log(`Link URL: ${info.linkUrl}`);
console.log(`Tab URL: ${tab.url}`);
const match = info.linkUrl.match(/^https?:\/\/myanimelist\.net\/(anime|manga)\/(\d+).*$/);
if (match) {
const type = match[1];
const id = match[2];
console.log("Match success");
console.log(`${type} ${id}`);
const generatedurl = {
"anime": [`https://myanimelist.net/ownlist/anime/add?selected_series_id=${id}`,
`https://myanimelist.net/ownlist/anime/${id}/edit`],
"manga": [`https://myanimelist.net/ownlist/manga/add?selected_manga_id=${id}`,
`https://myanimelist.net/ownlist/manga/${id}/edit`],
};
const maxattempts = 2;
const trytabs = (a) => {
if (a >= maxattempts) {
console.log("Probably need to log into MAL");
return;
}
console.log(`${a}`);
const malpromise = browser.tabs.create({ "index": tab.index + 1, "url": generatedurl[type][a] });
malpromise.then((maltab) => {
console.log(`${maltab.url}`);
console.log(`${generatedurl[type][a]}`);
let correctChange = false;
browser.tabs.onUpdated.addListener((tabId, changeInfo, _tabInfo) => {
if (tabId === maltab.id && !correctChange && changeInfo.url) {
correctChange = true;
if (changeInfo.url === generatedurl[type][a]) {
console.log("success");
browser.tabs.executeScript({ "file": "lib/browser-polyfill.js" });
browser.tabs.executeScript({ "file": "sourceadder.js" }).then((_result) => {
console.log("sending message");
browser.tabs.sendMessage(maltab.id, {
"taburl": tab.url,
"type": type
});
}, (err) => {
console.log(`failed running script due to err: ${err}`);
});
} else {
console.log("failure");
const removepromise = browser.tabs.remove(maltab.id);
removepromise.then(() => {
console.log("Tab closed");
trytabs(a + 1);
}, (e) => {
console.log(`Error in closing tab: ${e}`);
});
}
} else if (correctChange) {
console.log("changes are irrelevant now");
} else {
console.log("tab url hasn't changed yet");
}
});
}, (error) => {
console.log(`Error in #${a + 1} tab: ${error}`);
});
};
trytabs(0);
} else {
console.log("Match fail");
}
console.log("world");
}
});