Auto All Page

Otomatis menampilkan semua halaman artikel berita dalam 1 page

当前为 2021-01-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Auto All Page
  3. // @version 1.7.2
  4. // @author reforget-id
  5. // @namespace autoallpage
  6. // @icon https://www.iconsdb.com/icons/download/orange/pages-1-256.png
  7. // @homepage https://github.com/reforget-id/AutoAllPage
  8. // @description Otomatis menampilkan semua halaman artikel berita dalam 1 page
  9. // @include http*://*.detik.com/*
  10. // @include http*://*.kompas.com/*
  11. // @include http*://*.tribunnews.com/*
  12. // @include http*://*.merdeka.com/*
  13. // @include http*://*.suara.com/*
  14. // @include http*://*.matamata.com/*
  15. // @include http*://*.sindonews.com/read/*
  16. // @include http*://*.inews.id/*
  17. // @include http*://*.grid.id/read/*
  18. // @include http*://*.bolasport.com/read/*
  19. // @include http*://*.motorplus-online.com/read/*
  20. // @include http*://*.gridoto.com/read/*
  21. // @include http*://*.pikiran-rakyat.com/*
  22. // @include http*://*.kontan.co.id/news/*
  23. // @include http*://akurat.co/*
  24. // @include http*://m.akurat.co/*
  25. // @include http*://*.kompasiana.com/*
  26. // @include http*://*.cnbcindonesia.com/*
  27. // @include http*://*.republika.co.id/*
  28. // @include http*://*.jpnn.com/news/*
  29. // @grant GM_xmlhttpRequest
  30. // @run-at document-start
  31. // ==/UserScript==
  32.  
  33. 'use strict';
  34.  
  35. (() => {
  36.  
  37. let mainPage
  38. const url = window.location.href
  39. const log = '[AutoAllPage]'
  40.  
  41. const redirectRegex = {
  42. detik: /(?<=^.+\.detik\.com\/[a-z-]+\/d-\d+\/.+)((?<!\?.*|\/\d*)|\?.*(?<!\?single=1)|\/\d*)$/,
  43. kompas: /(?<=^.+\.kompas.com\/([a-z-]+\/|)read\/\d{4}\/\d{2}\/\d{2}\/\d+\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all(#page\d+|))|\/)$/,
  44. tribun: /(?<=^.+.tribunnews.com\/([a-z-]+\/|)\d{4}\/\d{2}\/\d{2}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  45. merdeka: /(?<=^.+\.merdeka\.com\/[a-z-]+\/.+\.html)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  46. suara: /(?<=^.+\.(suara|matamata)\.com\/[a-z-]+\/\d{4}\/\d{2}\/\d{2}\/\d+\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  47. sindo: /(?<=^.+\.sindonews\.com\/read\/\d+\/\d+\/.+)((?<!\?.*|\/)|\?.*(?<!\?showpage=all)|\/\d*)$/,
  48. inews: /(?<=^.+\.inews\.id\/(berita|[a-z-]+\/[a-z-]+)\/.+)((?<!\?.*|\/(all.*|\d*))|(\/all.+)|\/\d*|\?.*)$/,
  49. grid: /(?<=^.+\.(grid\.id|(motorplus-online|gridoto|bolasport)\.com)\/read\/\d+\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  50. pr: /(?<=^.+\.pikiran-rakyat\.com\/[a-z-]+\/pr-\d+\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  51. kontan: /(?<=^.+\.kontan\.co\.id\/news\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  52. akurat: /(?<=^.+akurat\.co\/[a-z-]+\/id-\d{7}-.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  53. mAkurat: /(?<=^.+m\.akurat\.co\/id-\d{7}-.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  54. kompasiana: /(?<=^.+\.kompasiana\.com\/.+\/[a-z0-9]{24}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all(#sectionall|))|\/)$/,
  55. cnbc: /(?<=^.+\.cnbcindonesia\.com\/[a-z-]+\/\d{14}-\d{1,2}-\d{4,6}\/.+)(\/([2-9]|\d{2})(\?.+|))$/,
  56. republika: /(?<=^.+\.republika\.co\.id\/berita\/[a-z0-9]+\/.+)(-part\d+.*)$/,
  57. jpnn: /(?<=^.+\.jpnn\.com\/news\/.+)(\?.+=.+)$/
  58. }
  59.  
  60. const xhrRegex = {
  61. cnbc: /(^.+\.cnbcindonesia\.com\/[a-z-]+\/\d{14}-\d{1,2}-\d{4,6}\/.+)/,
  62. republika: /(^.+\.republika\.co\.id\/berita\/[a-z0-9]+\/.+)/,
  63. jpnn: /(^.+\.jpnn\.com\/news\/.+)/
  64. }
  65.  
  66. for (let i in redirectRegex) {
  67. let urlMatcher = url.match(redirectRegex[i])
  68. if (urlMatcher) {
  69. redirector(i)
  70. break
  71. }
  72. }
  73.  
  74. function redirector(patternName) {
  75. let replacer, newUrl
  76.  
  77. if (patternName == 'detik') {
  78. replacer = '?single=1'
  79. } else if (patternName == 'sindo') {
  80. replacer = '?showpage=all'
  81. } else if (patternName == 'inews') {
  82. replacer = '/all'
  83. } else if (
  84. patternName == 'cnbc' ||
  85. patternName == 'republika' ||
  86. patternName == 'jpnn') {
  87. replacer = ''
  88. } else {
  89. replacer = '?page=all'
  90. }
  91.  
  92. newUrl = url.replace(redirectRegex[patternName], replacer)
  93. console.log(`EXECUTE [${patternName}] REDIRECT`)
  94. window.location.href = newUrl
  95. return
  96. }
  97.  
  98. window.addEventListener('DOMContentLoaded', () => {
  99. console.log(log, 'DOM fully loaded and parsed')
  100.  
  101. for (let i in xhrRegex) {
  102. let urlMatcher = url.match(xhrRegex[i])
  103. if (urlMatcher) {
  104. prepareXhr(i)
  105. break
  106. }
  107. }
  108. })
  109.  
  110. function prepareXhr(patternName) {
  111. if (patternName == 'cnbc') {
  112. cnbcXhr()
  113. } else if (patternName == 'republika') {
  114. republikaXhr()
  115. } else if (patternName == 'jpnn') {
  116. jpnnXhr()
  117. }
  118.  
  119. }
  120.  
  121. function findPagination(className) {
  122. const paging = document.getElementsByClassName(className)[0]
  123. if (!paging) console.log(log, 'Pagination is not found')
  124. return paging
  125. }
  126.  
  127. function createXhr(url, i, find) {
  128. console.log(log, `Creating XHR request for page ${i+1}`)
  129. return new Promise((resolve, reject) => {
  130. GM_xmlhttpRequest({
  131. method: 'GET',
  132. url: url,
  133. overrideMimeType: 'text/html; charset=UTF-8',
  134. responseType: 'document',
  135. binary: false,
  136. timeout: 0,
  137. withCredentials: true,
  138. onerror: () => {
  139. reject(console.log(log, 'Failed to create XHR request'))
  140. },
  141. onload: async (res) => {
  142. let text
  143. const hostname = window.location.hostname
  144.  
  145. if (hostname.includes('jpnn.com')) {
  146. text = res.response.querySelector(find)
  147. } else {
  148. text = res.response.getElementsByClassName(find)[0]
  149. }
  150.  
  151. if (text) {
  152. console.log(log, `Success get text of page ${i+1} from XHR`)
  153. mainPage.appendChild(text)
  154. //mainPage.after(text)
  155. resolve(console.log(log, `Append page ${i+1} to main page`))
  156. } else {
  157. reject(console.log(log, 'Failed to get text XHR'))
  158. }
  159. }
  160. })
  161. })
  162. }
  163.  
  164. async function cnbcXhr() {
  165. const pagination = findPagination('paging-circle')
  166. if (!pagination) return
  167.  
  168. const pagingParent = pagination.parentNode
  169. pagingParent.style.display = 'none'
  170. pagingParent.previousElementSibling.style.display = 'none'
  171. mainPage = document.getElementsByClassName('detail_text')[0].parentNode
  172.  
  173. for (let i = 1; i < pagination.childElementCount; i++) {
  174. let href = pagination.children[i].getAttribute('href')
  175. await createXhr(href, i, 'detail_text')
  176. }
  177. }
  178.  
  179. async function republikaXhr() {
  180. const pagination = findPagination('pagination')
  181. if (!pagination) return
  182.  
  183. const pageChildren = pagination.getElementsByTagName('a')
  184. mainPage = document.getElementsByClassName('teaser_detail')[0]
  185. pagination.style.display = 'none'
  186.  
  187. for (let i = 1; i < pageChildren.length - 1; i++) {
  188. let href = pageChildren[i].getAttribute('href')
  189. await createXhr(href, i, 'artikel')
  190. }
  191.  
  192. const bacaJuga = document.getElementsByClassName('baca-juga')
  193. for (let i = 0; i < bacaJuga.length; i++) {
  194. bacaJuga[i].remove()
  195. i--
  196. }
  197. }
  198.  
  199. async function jpnnXhr() {
  200. const pagination = findPagination('pagination')
  201. if (!pagination) return
  202.  
  203. const pageChildren = pagination.getElementsByTagName('a')
  204. mainPage = document.querySelector('[itemprop=articleBody]')
  205. pagination.style.display = 'none'
  206.  
  207. for (let i = 1; i < pageChildren.length - 1; i++) {
  208. let href = pageChildren[i].getAttribute('href')
  209. await createXhr(href, i, '[itemprop=articleBody]')
  210. }
  211. }
  212.  
  213. })()