Skip to content

Commit

Permalink
Re-work based on sveltejs/svelte#3005
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Johnson committed Jun 11, 2019
1 parent e31a5ee commit 3f4a0bf
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 285 deletions.
76 changes: 1 addition & 75 deletions dest/background.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,3 @@
function instrument(str) {
if (!str.includes('generated by Svelte v3.')) {
return false
}
return str
.replace(
'$$.fragment = create_fragment($$.ctx)',
'$&\ndocument.dispatchEvent(new CustomEvent("SvelteRegisterComponent", { detail: { component, tagName: component.constructor.name } }))'
)
.replace(
/(function\s+instance(?:\$\d+)?\(\s*\$\$self\s*,\s*\$\$props\s*,\s*\$\$invalidate\s*\)\s*{[^]+?)(return\s*{\s*((?:\w+\s*,?\s*)+)}\s*;?\s*}\s*class\s+\w+\s+extends\s+SvelteComponent\s+{)/g,
(_, pre, post, ctx) =>
`${pre}$$self.$unsafe_set = $$values => {${ctx
.split(/(?:,\s*|\s+)/)
.map(key =>
key == ''
? ''
: `if ('${key}' in $$values) $$invalidate('${key}', ${key} = $$values.${key})`
)
.join('\n')}}
${post}`
)
.replace(
/(\/\/.+?({(?:#|:).+})?\s+function\s+create_((?:each|if|else|pending|then|catch)_block(?:_\d+?)?(?:\$\d+?)?)\s*\(\s*ctx\s*\)\s*{[^]+?)return\s+({[^]+?}\s*;)\s*}/g,
(_, fn, source, blockId, block) => `${fn}
const block = ${block}
document.dispatchEvent(new CustomEvent("SvelteRegisterBlock", { detail: { block, blockId: '${blockId}', source: "${
source ? source.replace(/"/g, '\\"') : ''
}", ctx } }))
return block
}`
)
.replace(
/(.+listen\s*\(\s*(\w+)\s*,\s*("\w+")\s*,\s*(stop_propagation\s*\()?\s*(prevent_default\s*\()?)(.+?)(\)?\s*\)?\s*,?\s*(?:{(.+?)}|(true))\s*\)\s*;)/g,
(
_,
left,
element,
type,
stopPropagation,
preventDefault,
handler,
right,
options,
capture
) =>
`const ___handler = ${handler}
${left}___handler${right}
document.dispatchEvent(new CustomEvent("SvelteAddEventListener", { detail: { type: ${type}, element: ${element}, handler: ___handler, options: {
${capture ? 'capture: true,' : ''}
${stopPropagation ? 'stopPropagation: true,' : ''}
${preventDefault ? 'preventDefault: true,' : ''}
${options || ''}
}}}))`
)
}

const toolsPorts = new Map()
const pagePorts = new Map()

Expand Down Expand Up @@ -121,7 +64,7 @@ function queueInlineScript(tabId, url) {
inlineScripts.push(
fetch(url)
.then(o => o.text())
.then(o => ({ tabId, url, source: instrument(o) }))
.then(source => ({ tabId, url, source }))
)
}

Expand All @@ -135,23 +78,6 @@ function interceptRequest(details) {
return { cancel: true }
}

const filter = chrome.webRequest.filterResponseData(details.requestId)
const decoder = new TextDecoder('utf-8')
const encoder = new TextEncoder()

filter.ondata = event => {
const str = decoder.decode(event.data, { stream: true })
const instrumented = instrument(str)
if (!instrumented) {
filter.write(event.data)
filter.disconnect()
return
}

filter.write(encoder.encode(instrumented))
filter.disconnect()
}

return {}
}

Expand Down
Loading

0 comments on commit 3f4a0bf

Please sign in to comment.