detect.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. md.detect = ({storage: {state}, inject}) => {
  2. var onwakeup = true
  3. var ff = (id, info, done) => {
  4. if (chrome.runtime.getBrowserInfo === undefined) {
  5. // chrome
  6. done('load')
  7. }
  8. else {
  9. var manifest = chrome.runtime.getManifest()
  10. if (manifest.browser_specific_settings && manifest.browser_specific_settings.gecko) {
  11. if (!info.url) {
  12. done('noop')
  13. }
  14. else {
  15. chrome.tabs.sendMessage(id, {message: 'ping'})
  16. .then(() => done('noop'))
  17. .catch(() => done('load'))
  18. }
  19. }
  20. else {
  21. done('load')
  22. }
  23. }
  24. }
  25. var tab = (id, info, tab) => {
  26. if (info.status === 'loading') {
  27. ff(id, info, (action) => {
  28. if (action === 'noop') {
  29. return
  30. }
  31. // try
  32. chrome.scripting.executeScript({
  33. target: {tabId: id},
  34. func: () =>
  35. JSON.stringify({
  36. url: window.location.href,
  37. header: document.contentType,
  38. loaded: !!window.state,
  39. })
  40. }, (res) => {
  41. if (chrome.runtime.lastError) {
  42. // origin not allowed
  43. return
  44. }
  45. try {
  46. var win = JSON.parse(res[0].result)
  47. if (!win) {
  48. return
  49. }
  50. }
  51. catch (err) {
  52. // JSON parse error
  53. return
  54. }
  55. if (win.loaded) {
  56. // anchor
  57. return
  58. }
  59. if (detect(win.header, win.url)) {
  60. if (onwakeup && chrome.webRequest) {
  61. onwakeup = false
  62. chrome.tabs.reload(id)
  63. }
  64. else {
  65. inject(id)
  66. }
  67. }
  68. })
  69. })
  70. }
  71. }
  72. var detect = (content, url) => {
  73. var location = new URL(url)
  74. var origin =
  75. state.origins[location.origin] ||
  76. state.origins[location.protocol + '//' + location.hostname] ||
  77. state.origins[location.protocol + '//' + location.host] ||
  78. state.origins[location.protocol + '//*.' + location.hostname.replace(/^[^.]+\.(.*)/, '$1')] ||
  79. state.origins[location.protocol + '//*.' + location.host.replace(/^[^.]+\.(.*)/, '$1')] ||
  80. state.origins['*://' + location.hostname] ||
  81. state.origins['*://' + location.host] ||
  82. state.origins['*://*.' + location.hostname.replace(/^[^.]+\.(.*)/, '$1')] ||
  83. state.origins['*://*.' + location.host.replace(/^[^.]+\.(.*)/, '$1')] ||
  84. state.origins['*://*']
  85. return (
  86. (origin && origin.header && origin.path && origin.match && /\btext\/(?:(?:(?:x-)?markdown)|plain)\b/i.test(content) && new RegExp(origin.match).test(location.href)) ||
  87. (origin && origin.header && !origin.path && /\btext\/(?:(?:(?:x-)?markdown)|plain)\b/i.test(content)) ||
  88. (origin && origin.path && origin.match && !origin.header && new RegExp(origin.match).test(location.href))
  89. ? origin
  90. : undefined
  91. )
  92. }
  93. return {tab}
  94. }