Auto All Page

Otomatis menampilkan semua halaman artikel berita dalam 1 page

当前为 2021-07-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Auto All Page
  3. // @version 1.8.1
  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*://*.kompasiana.com/*
  25. // @include http*://*.cnbcindonesia.com/*
  26. // @include http*://*.republika.co.id/*
  27. // @include http*://creativedisc.com/*
  28. // @include http*://*.okezone.com/read/*
  29. // @include http*://*.viva.co.id/*
  30. // @include http*://*.kompas.tv/*
  31. // @include http*://*.wartaekonomi.co.id/*
  32. // @include http*://herstory.co.id/*
  33. // @include http*://*.sonora.id/*
  34. // @include http*://*.tvonenews.com/*
  35. // @include http*://*.intipseleb.com/*
  36. // @include http*://*.sahijab.com/*
  37. // @include http*://*.jagodangdut.com/*
  38. // @include http*://*.100kpj.com/*
  39. // @grant GM_xmlhttpRequest
  40. // @run-at document-start
  41. // ==/UserScript==
  42.  
  43. 'use strict';
  44.  
  45. (() => {
  46. // http*://*.jpnn.com/news/*
  47.  
  48. let mainPage
  49. const url = window.location.href
  50. const log = '[AutoAllPage]'
  51.  
  52. const redirectRegex = {
  53. detik: /(?<=^.+\.detik\.com\/[a-z-]+\/d-\d{7,}\/.+)((?<!\?.*|\/\d*)|\?.*(?<!\?single=1)|\/\d*)$/,
  54. kompas: /(?<=^.+\.kompas\.com\/([a-z-]+\/|)read\/\d{4}\/\d{2}\/\d{2}\/\d+\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all(#page\d+|))|\/)$/,
  55. tribun: /(?<=^.+.tribunnews\.com\/([a-z-]+\/|)\d{4}\/\d{2}\/\d{2}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  56. merdeka: /(?<=^.+\.merdeka\.com\/[a-z-]+\/.+\.html)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  57. suara: /(?<=^.+\.(suara|matamata)\.com\/[a-z-]+\/\d{4}\/\d{2}\/\d{2}\/\d{6,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  58. sindo: /(?<=^.+\.sindonews\.com\/read\/\d{6,}\/\d{2,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?showpage=all)|\/\d*)$/,
  59. inews: /(?<=^.+\.inews\.id\/(berita|[a-z-]+\/[a-z-]+)\/.+)((?<!\?.*|\/(all.*|\d*))|(\/all.+)|\/\d*|\?.*)$/,
  60. grid: /(?<=^.+\.(grid\.id|(motorplus-online|gridoto|bolasport)\.com)\/read\/\d{8,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  61. pr: /(?<=^.+\.pikiran-rakyat\.com\/[a-z-]+\/pr-\d{7,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  62. kontan: /(?<=^.+\.kontan\.co\.id\/news\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  63. akurat: /(?<=^.+akurat\.co\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  64. kompasiana: /(?<=^.+\.kompasiana\.com\/.+\/[a-z0-9]{24}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all(#sectionall|))|\/)$/,
  65. viva: /(?<=^.+\.viva\.co\.id\/([a-z-]+(\/[a-z-]+|))\/\d{6,}-.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  66. cnbc: /(?<=^.+\.cnbcindonesia\.com\/[a-z-]+\/\d{14}-\d{1,}-\d{4,}\/.+)(\/([2-9]|\d{2})(\?.+|))$/,
  67. republika: /(?<=^.+\.republika\.co\.id\/berita\/[a-z0-9]+\/.+)(-part\d+.*)$/,
  68. jpnn: /(?<=^.+\.jpnn\.com\/news\/.+)(\?.+=.+)$/,
  69. cd: /(?<=^.+creativedisc\.com\/\d{4}\/\d{2}\/.+)(\/\d+\/.*)$/,
  70. okezone: /(?<=^.+\.okezone\.com\/read\/\d{4}\/\d{2}\/\d{2}\/\d{1,}\/\d{6,}\/.+)(\?page=([2-9]|\d{2}).*)$/,
  71. kompastv: /(?<=^.+\.kompas\.tv\/article\/\d{4,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  72. warkom: /(?<=^.+\.wartaekonomi\.co\.id\/read\d{5,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/ ,
  73. herstory: /(?<=^.+herstory\.co\.id\/read\d{4,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  74. sonora: /(?<=^.+\.sonora\.id\/read\/\d{8,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
  75. tvone: /(?<=^.+\.(tvonenews|intipseleb|sahijab|jagodangdut|100kpj)\.com\/([a-z-]+(\/[a-z-]+|))\/\d{3,}-.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/
  76. }
  77.  
  78. const xhrRegex = {
  79. cnbc: /(^.+\.cnbcindonesia\.com\/[a-z-]+\/\d{14}-\d{1,}-\d{4,}\/.+)/,
  80. republika: /(^.+\.republika\.co\.id\/berita\/[a-z0-9]+\/.+)/,
  81. jpnn: /(^.+\.jpnn\.com\/news\/.+)/,
  82. cd: /(^.+creativedisc\.com\/\d{4}\/\d{2}\/.+)/,
  83. okezone: /(^.+\.okezone\.com\/read\/\d{4}\/\d{2}\/\d{2}\/\d{1,}\/\d{6,}\/.+)/
  84. }
  85.  
  86. for (let i in redirectRegex) {
  87. let urlMatcher = url.match(redirectRegex[i])
  88. if (urlMatcher) {
  89. redirector(i)
  90. break
  91. }
  92. }
  93.  
  94. function redirector(patternName) {
  95. let replacer, newUrl
  96.  
  97. if (patternName == 'detik') {
  98. replacer = '?single=1'
  99. } else if (patternName == 'sindo') {
  100. replacer = '?showpage=all'
  101. } else if (patternName == 'inews') {
  102. replacer = '/all'
  103. } else if (
  104. patternName == 'cnbc' ||
  105. patternName == 'republika' ||
  106. patternName == 'jpnn' ||
  107. patternName == 'cd' ||
  108. patternName == 'okezone') {
  109. replacer = ''
  110. } else {
  111. replacer = '?page=all'
  112. }
  113.  
  114. newUrl = url.replace(redirectRegex[patternName], replacer)
  115. console.log(`EXECUTE [${patternName}] REDIRECT`)
  116. window.location.href = newUrl
  117. return
  118. }
  119.  
  120. window.addEventListener('DOMContentLoaded', () => {
  121. console.log(log, 'DOM fully loaded and parsed')
  122.  
  123. for (let i in xhrRegex) {
  124. let urlMatcher = url.match(xhrRegex[i])
  125. if (urlMatcher) {
  126. prepareXhr(i)
  127. break
  128. }
  129. }
  130. })
  131.  
  132. function prepareXhr(patternName) {
  133. switch(patternName) {
  134. case 'cnbc' :
  135. cnbcXhr()
  136. break
  137. case 'republika' :
  138. republikaXhr()
  139. break
  140. case 'jpnn' :
  141. jpnnXhr()
  142. break
  143. case 'cd' :
  144. cdXhr()
  145. break
  146. case 'okezone' :
  147. okezoneXhr()
  148. }
  149.  
  150. /*
  151. if (patternName == 'cnbc') {
  152. cnbcXhr()
  153. } else if (patternName == 'republika') {
  154. republikaXhr()
  155. } else if (patternName == 'jpnn') {
  156. jpnnXhr()
  157. }*/
  158. }
  159.  
  160. function findPagination(className) {
  161. const paging = document.getElementsByClassName(className)[0]
  162. if (!paging) console.log(log, 'Pagination is not found')
  163. return paging
  164. }
  165.  
  166. function createXhr(url, i, find) {
  167. console.log(log, `Creating XHR request for page ${i+1}`)
  168. return new Promise((resolve, reject) => {
  169. GM_xmlhttpRequest({
  170. method: 'GET',
  171. url: url,
  172. overrideMimeType: 'text/html; charset=UTF-8',
  173. responseType: 'document',
  174. binary: false,
  175. timeout: 0,
  176. withCredentials: true,
  177. onerror: () => {
  178. reject(console.log(log, 'Failed to create XHR request'))
  179. },
  180. onload: async (res) => {
  181. let text
  182. const hostname = window.location.hostname
  183.  
  184. if (hostname.includes('jpnn.com') || hostname.includes('creativedisc.com')) {
  185. text = res.response.querySelector(find)
  186. } else {
  187. text = res.response.getElementsByClassName(find)[0]
  188. }
  189.  
  190. if (text) {
  191. console.log(log, `Success get text of page ${i+1} from XHR`)
  192. if (hostname.includes('okezone.com')) {
  193. mainPage.after(text)
  194. } else {
  195. mainPage.appendChild(text)
  196. }
  197. resolve(console.log(log, `Append page ${i+1} to main page`))
  198. } else {
  199. reject(console.log(log, 'Failed to get text XHR'))
  200. }
  201. }
  202. })
  203. })
  204. }
  205.  
  206. async function cnbcXhr() {
  207. const pagination = findPagination('paging-circle')
  208. if (!pagination) return
  209.  
  210. const pagingParent = pagination.parentNode
  211. pagingParent.style.display = 'none'
  212. pagingParent.previousElementSibling.style.display = 'none'
  213. mainPage = document.getElementsByClassName('detail_text')[0].parentNode
  214.  
  215. for (let i = 1; i < pagination.childElementCount; i++) {
  216. let href = pagination.children[i].getAttribute('href')
  217. await createXhr(href, i, 'detail_text')
  218. }
  219. }
  220.  
  221. async function republikaXhr() {
  222. const pagination = findPagination('pagination')
  223. if (!pagination) return
  224.  
  225. const pageChildren = pagination.getElementsByTagName('a')
  226. mainPage = document.getElementsByClassName('teaser_detail')[0]
  227. pagination.style.display = 'none'
  228.  
  229. for (let i = 1; i < pageChildren.length - 1; i++) {
  230. let href = pageChildren[i].getAttribute('href')
  231. await createXhr(href, i, 'artikel')
  232. }
  233.  
  234. const bacaJuga = document.getElementsByClassName('baca-juga')
  235. for (let i = 0; i < bacaJuga.length; i++) {
  236. bacaJuga[i].remove()
  237. i--
  238. }
  239. }
  240.  
  241. async function jpnnXhr() {
  242. const pagination = findPagination('pagination')
  243. if (!pagination) return
  244.  
  245. const pageChildren = pagination.getElementsByTagName('a')
  246. mainPage = document.querySelector('[itemprop=articleBody]')
  247. pagination.style.display = 'none'
  248.  
  249. for (let i = 1; i < pageChildren.length - 1; i++) {
  250. let href = pageChildren[i].getAttribute('href')
  251. await createXhr(href, i, '[itemprop=articleBody]')
  252. }
  253. }
  254.  
  255. async function cdXhr() {
  256. const pagination = findPagination('post-nav-links')
  257. if (!pagination) return
  258.  
  259. const pageChildren = pagination.getElementsByTagName('a')
  260. mainPage = document.querySelector('[itemprop=articleBody]').parentNode
  261. pagination.style.display = 'none'
  262.  
  263. for (let i = 0; i < pageChildren.length; i++) {
  264. let href = pageChildren[i].getAttribute('href')
  265. await createXhr(href, i+1, '[itemprop=articleBody]')
  266. }
  267. }
  268.  
  269. async function okezoneXhr() {
  270. const pagination = document.getElementsByClassName('paging')
  271. if (!pagination.length) {
  272. console.log(log, 'Pagination is not found')
  273. return
  274. }
  275.  
  276. const pageChildren = pagination[0].getElementsByTagName('a')[0]
  277. const href = pageChildren.getAttribute('href')
  278. mainPage = document.getElementsByClassName('read')[0]
  279.  
  280. if (href === '#') return
  281. await createXhr(href, 1, 'read')
  282.  
  283. const detailTag = document.getElementsByClassName('detail-tag')
  284. for (let i = 0; i < pagination.length; i++) {
  285. pagination[i].style.display = 'none'
  286. detailTag[i].style.display = 'none'
  287. }
  288. }
  289.  
  290. })()