Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

リッチテキスト形式でコピーするようにした #1

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.github.akasakashota'
version 'v1.1.0'
version 'v1.2.0'

repositories {
mavenCentral()
Expand Down
74 changes: 66 additions & 8 deletions src/main/kotlin/BurpExtender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package burp

import burp.IRequestInfo.*
import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.StringSelection
import java.awt.datatransfer.*
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import java.io.ByteArrayInputStream
import java.io.IOException
import java.io.PrintWriter
import java.lang.Exception
import java.lang.RuntimeException
import javax.swing.JMenuItem
import kotlin.collections.MutableList
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableModel


class BurpExtender : IBurpExtender, IContextMenuFactory {
Expand Down Expand Up @@ -53,9 +53,17 @@ class BurpExtender : IBurpExtender, IContextMenuFactory {
private inner class CopyListener(private val formattedItems: List<Formatting>) : MouseListener {
override fun mouseReleased(e: MouseEvent?) {
try {
val model = DefaultTableModel(0, 4)
for (item in formattedItems) {
model.addRow(item.toArray())
}

val clip: Clipboard = Toolkit.getDefaultToolkit()!!.systemClipboard
val ss = StringSelection(formattedItems.joinToString("\n"))
clip.setContents(ss, ss)
clip.setContents(
TableTransferable(model),
ClipboardOwner { _, _ -> println("lost")}
)

out("Copied ${formattedItems.size} item(s).")
} catch (e: Exception) { err(e) }
}
Expand Down Expand Up @@ -115,7 +123,57 @@ class BurpExtender : IBurpExtender, IContextMenuFactory {
}

override fun toString(): String {
return arrayOf(method, url, requestBody, responseBody).joinToString("\t")
return toArray().joinToString("\t")
}

public fun toArray(): Array<String> {
return arrayOf(method, url, requestBody, responseBody)
}
}

class TableTransferable(private val table: TableModel) : Transferable {
companion object {
val HTML_DATA_FLAVOR = DataFlavor("text/html", "HTML")
}

override fun getTransferDataFlavors(): Array<DataFlavor> {
return arrayOf(HTML_DATA_FLAVOR)
}

override fun isDataFlavorSupported(flavor: DataFlavor): Boolean {
return transferDataFlavors.any {
it == flavor
}
}

@Throws(UnsupportedFlavorException::class, IOException::class)
override fun getTransferData(flavor: DataFlavor): Any {
val data: Any = if (HTML_DATA_FLAVOR.equals(flavor)) {
ByteArrayInputStream(formatAsHTML().toByteArray())
} else {
throw UnsupportedFlavorException(flavor)
}
return data
}

private fun formatAsHTML(): String {
val builder = StringBuilder()

builder.append("<table>")
for (i in 0 until table.rowCount) {
builder.append("<tr>")
for (j in 0 until table.columnCount) {
val v = table.getValueAt(i, j)
builder
.append("<td>")
.append(v?.toString() ?: "")
.append("</td>")
}
builder.append("</tr>")
}
builder.append("</table>")

return builder.toString()
}
}

Expand Down