MangaUpdates chapter links

Allows to add direct links to chapters in MangaUpdates release lists

当前为 2019-09-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MangaUpdates chapter links
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Allows to add direct links to chapters in MangaUpdates release lists
  6. // @author You
  7. // @match https://www.mangaupdates.com/*
  8. // @require https://cdnjs.cloudflare.com/ajax/libs/mithril/2.0.4/mithril.min.js
  9. // @require https://cdn.jsdelivr.net/npm/coffeescript@2.4.1/lib/coffeescript-browser-compiler-legacy/coffeescript.js
  10. // @grant GM_addStyle
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // ==/UserScript==
  14.  
  15. var inline_src = String.raw`
  16. GM_addStyle "html {background: black} body {background: initial}
  17. .center-side-bar {background: rgba(0, 0, 0, 0.1)}
  18. button.inbox.-edit {height: 15px; margin-right: 3px; cursor: pointer}
  19. button.inbox[disabled] {opacity: .5; cursor: not-allowed}
  20. button.-edit.-none {background: darkgrey} a.-chapter:not([href]) {color: darkgrey}
  21. .-overlay {position: fixed; top: 0; left: 0; height: 100%; width: 100%; z-index: 10000; pointer-events: none}
  22. .-dialog {pointer-events: all; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  23. background: rgba(0, 0, 0, .8); color: white; padding: 2em}
  24. .-row {margin: 20px; display: block} .-buttons {display: flex; justify-content: space-between}
  25. .-dialog input {width: 500px}"
  26.  
  27. $merge = Object.assign
  28. merge = (os) -> $merge {}, ...os
  29. fromPairs = (xs) -> merge xs.map ([k, v]) -> k and [k]: v
  30. qstr = (s) -> if not s.includes('?') then "" else s[1 + s.indexOf '?'..]
  31. query = (s) -> fromPairs (z.split('=').map(decodeURIComponent) for z in qstr(s).split('&') when z.includes '=')
  32. chunks = (n, l) -> (l[i ... i+n] for i in [0...l.length] by n)
  33. fmt = (s, o) -> Object.keys(o).reduce ((s, k) -> s.replace '#{'+k+'}', o[k]), s
  34.  
  35. $e = (tag, options...) -> $merge document.createElement(tag), options...
  36. $get = (xpath, e=document) -> document.evaluate(xpath, e, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
  37. $find = (selector, e=document) -> e.querySelector selector
  38. $find_ = (selector, e=document) -> Array.from e.querySelectorAll selector
  39. fullWidth = (e) -> e.classList.contains 'col-12'
  40.  
  41. URI = window.location.pathname
  42. QUERY = query window.location.search
  43.  
  44. releaseTable = switch
  45. when URI is "/releases.html" then $get("div[2]/div", main_content)
  46. when URI is "/groups.html" and 'id' of QUERY then $get("div/div[3]/div[2]/div/div", main_content)
  47.  
  48. if releaseTable
  49. console.log releaseTable
  50. overlay = $e('div', className: '-overlay')
  51. document.body.appendChild overlay
  52.  
  53. state = editing: null
  54.  
  55. startEditing = (name, args...) -> -> $merge(state, editing: name, args...); m.redraw()
  56. closeDialog = -> $merge(state, {editing: null})
  57. editName = -> switch state.editing
  58. when 'groupUrl' then "Group URL (#{state.groupName})"
  59. when 'titleUri' then "Title ID (#{state.titleName})"
  60. saveChanges = ->
  61. cfg = GM_getValue(state.groupId, {})
  62. switch state.editing
  63. when 'groupUrl' then cfg.url = state.value
  64. when 'titleUri' then cfg.titles = $merge cfg.titles or {}, {[state.titleId]: state.value}
  65. GM_setValue state.groupId, cfg
  66. closeDialog()
  67. setTimeout recalc
  68.  
  69. chapterNumber = (s) -> s.match(/\d+/)?[0]
  70. $ensureButton = (e) ->
  71. btn = $find 'button', e
  72. unless btn
  73. btn = $e('button', className: "inbox -edit", title: "Edit", innerText: '*')
  74. e.insertBefore btn, e.firstChild
  75. btn
  76. $ensureHref = (e) ->
  77. href = $find 'a', e
  78. unless href
  79. href = $e('a', className: '-chapter', target: '_blank')
  80. href.appendChild e.firstChild while e.firstChild
  81. e.appendChild href
  82. href
  83.  
  84. recalc = ->
  85. _configs = {}
  86. config = (id) -> _configs[id] or (_configs[id] = GM_getValue(id, {}))
  87. items = $find_ ":scope > *", releaseTable
  88. separator = items.findIndex fullWidth
  89. columns = items[...separator].map (e) -> e.innerText.trim()
  90. _items = items[separator+1..]
  91. pager = _items.findIndex fullWidth
  92. cells = _items[...pager]
  93. rows = chunks columns.length, cells
  94. title = columns.indexOf "Title"
  95. chapter = columns.indexOf "Chp"
  96. group = columns.indexOf "Groups"
  97. for row in rows
  98. [titleName, groupName] = [title, group].map (k) -> row[k].innerText.replace(/^\*/, '')
  99. [titleId, groupId] = [title, group].map (k) -> query($find('a', row[k])?.href or "").id
  100. chapterId = chapterNumber row[chapter].innerText.trim()
  101. cfg = config [groupId]
  102. titleUri = cfg.titles?[titleId]
  103. [titleBtn, groupBtn] = [row[title], row[group]].map $ensureButton
  104. groupBtn.classList[if cfg.url then 'remove' else 'add']('-none')
  105. groupBtn.onclick = startEditing 'groupUrl', {groupId, groupName}, value: cfg.url or ""
  106. titleBtn.classList[if titleUri then 'remove' else 'add']('-none')
  107. titleBtn.onclick = startEditing 'titleUri', {groupId, groupName, titleId, titleName}, value: titleUri or ""
  108. titleBtn.disabled = not titleId
  109. chapterLink = $ensureHref row[chapter]
  110. if cfg.url and titleUri
  111. chapterLink.href = fmt(cfg.url, chapter: chapterId, title: titleUri)
  112. chapterLink.classList.add '-none'
  113. else
  114. chapterLink.removeAttribute 'href'
  115. chapterLink.classList.remove '-none'
  116.  
  117. recalc()
  118.  
  119. m.mount overlay, view: -> state.editing and m '.-dialog', [
  120. m 'label.-row', editName(state.editing),
  121. m 'input.inbox.-row', value: state.value, oninput: -> state.value = @value
  122. m '.-buttons.-row',
  123. m 'button.inbox', {onclick: closeDialog}, "Cancel"
  124. m 'button.inbox', {onclick: saveChanges}, "Ok"
  125. ]
  126. `;
  127. eval( CoffeeScript.compile(inline_src) );