Skip to content

Commit

Permalink
fix selector for icml2023 (openreview.net)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinXueyuanStdio committed Mar 4, 2023
1 parent 2abadd2 commit bb9f43b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 74 deletions.
34 changes: 24 additions & 10 deletions src/content-script/ChatGPTQueryBuilder.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { ElementInterface } from './interface-configs.js'
import { promptTemplates } from './promptTemplates.js'

export function buildReviewQuery(siteName: string, paperContainerElement: Element): string {
export function buildReviewQuery(siteName: string, siteConfig: ElementInterface): string {
const type = "review"
let paperTitle: string = ""
let paperAbstract: string = ""
const titleNode = paperContainerElement.querySelector(".title_pdf_row > .note_content_title")
paperTitle = titleNode?.textContent ?? ""

// get title
const titleElement = document.querySelector(siteConfig.titleElement)
if (!titleElement) {
console.error("no title found: " + siteConfig.titleElement)
return ""
}
paperTitle = titleElement?.textContent ?? ""

// get abstract
if (siteName === "notebook") {
const node_contents = paperContainerElement.querySelectorAll(".note_contents") ?? []
const node_contents = document.querySelectorAll(siteConfig.abstractElement) ?? []
for (const node of node_contents) {
const node_title = node.querySelector(".note_content_field")
if (!node_title) continue
Expand All @@ -19,9 +27,15 @@ export function buildReviewQuery(siteName: string, paperContainerElement: Elemen
paperAbstract = abstract?.textContent ?? ""
}
}
} else {
return ""
} else if (siteName === "icml2023") {
const abstractElement = document.querySelector(siteConfig.abstractElement)
if (!abstractElement) {
console.error("no abstract found: " + siteConfig.abstractElement)
return ""
}
paperAbstract = abstractElement?.textContent ?? ""
}

const query = promptTemplates[type](paperTitle, paperAbstract)
return query
}
Expand All @@ -44,10 +58,10 @@ const fuzzyKeys: Record<string, string[]> = {

export function buildRebuttalQuery(siteName: string, reviewContainerElement: Element): string {
const type = "rebuttal"
let paperSummary : string = ""
let paperSummaryOfStrengths : string = ""
let paperSummaryOfWeaknesses : string = ""
let paperSuggestions : string = ""
let paperSummary: string = ""
let paperSummaryOfStrengths: string = ""
let paperSummaryOfWeaknesses: string = ""
let paperSuggestions: string = ""

if (siteName === "notebook") {
const node_contents = reviewContainerElement.querySelectorAll(".note_contents") ?? []
Expand Down
9 changes: 0 additions & 9 deletions src/content-script/Interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,3 @@ export function Button(props: Props) {
)

}

export function LabArea() {
return (
<div className="lm-Widget p-Widget jp-PropertyInspector lm-mod-hidden p-mod-hidden lm-StackedPanel-child p-StackedPanel-child" style={{height: '100%'}} id="labContainerParent">
<div className="lm-Widget p-Widget jp-NotebookTools" id="labContainerChild"></div>
</div>
)

}
68 changes: 27 additions & 41 deletions src/content-script/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'github-markdown-css'
import { render } from 'preact'
import ChatGPTQuery from './ChatGPTQuery'
import { buildReviewQuery, buildRebuttalQuery, queryReviewElements } from './ChatGPTQueryBuilder'
import { Button, LabArea } from './Interface'
import { Button } from './Interface'
import { config, ElementInterface } from './interface-configs.js'
import './styles.scss'
import { promptSettings } from './prompt-configs.js'
Expand All @@ -12,8 +12,8 @@ import { CopilotIcon } from '@primer/octicons-react'
/* Functions */
/* -------------------------------------------------------------------------- */

async function identify_notebook_type(config: Record<string, ElementInterface>) {
const maxTries = 14
async function identify_inject_type(config: Record<string, ElementInterface>) {
const maxTries = 30
let tries = 0
let parent = null
while (tries < maxTries) {
Expand Down Expand Up @@ -69,26 +69,12 @@ export async function submit_and_add_question(
)
}

function attach_lab_area() {
const parentArea = document.querySelector('#jp-left-stack')
const area_container = document.createElement('span')
area_container.className = 'chatgpt-area'
parentArea!.append(area_container)
if (!parentArea) {
console.error("ChatGPT Review&Rebuttal: Error - could not find parent area")
return
}
render(
<LabArea />,
area_container
)
}

function create_review_button(siteConfig: ElementInterface, siteName: string) {
const key = "review"
const prompt = promptSettings[key]
const paperElement = document.querySelector("#content > .forum-container > .note")
if (!paperElement) {
const resultContainerElement = document.querySelector(siteConfig.resultContainer)
if (!resultContainerElement) {
console.error("no container found to append results: " + siteConfig.resultContainer)
return
}

Expand All @@ -107,8 +93,8 @@ function create_review_button(siteConfig: ElementInterface, siteName: string) {
<Button
name={prompt.buttonLabel}
onClick={() => {
const query = buildReviewQuery(siteName, paperElement)
submit_and_add_question(query, key, paperElement)
const query = buildReviewQuery(siteName, siteConfig)
submit_and_add_question(query, key, resultContainerElement)
}}
icon={prompt.buttonIcon}
/>
Expand Down Expand Up @@ -154,22 +140,28 @@ function create_rebuttal_button(siteConfig: ElementInterface, siteName: string)
}
}

async function create_interface_awaiting_loading(siteConfig: ElementInterface, siteName: string) {
const maxTries = 30
let tries = 0

while (tries < maxTries) {
const loadingElement = document.querySelector(siteConfig.loadingElement)
if (!loadingElement) {
create_review_button(siteConfig, siteName)
create_rebuttal_button(siteConfig, siteName)
return
}
await new Promise(resolve => setTimeout(resolve, 500))
}
}

async function create_interface(siteConfig: ElementInterface, siteName: string) {
// create review and rebuttal button
// Attach manual close to make sure the closing button still works in openreview
if (siteName === "notebook") {
const maxTries = 14
let tries = 0

while (tries < maxTries) {
const loadingElement = document.querySelector("#note_children > .spinner-container")
if (!loadingElement) {
create_review_button(siteConfig, siteName)
create_rebuttal_button(siteConfig, siteName)
return
}
await new Promise(resolve => setTimeout(resolve, 500))
}
await create_interface_awaiting_loading(siteConfig, siteName)
} else if (siteName === "icml2023") {
await create_interface_awaiting_loading(siteConfig, siteName)
}
}

Expand All @@ -179,18 +171,12 @@ async function create_interface(siteConfig: ElementInterface, siteName: string)

async function main() {

const siteName = await identify_notebook_type(config)
const siteName = await identify_inject_type(config)
if (siteName) {
// Get the config for the current site
const siteConfig = config[siteName]

// Add the extension interface
await create_interface(siteConfig, siteName)

if (siteName === "lab") {
setTimeout(() => { attach_lab_area() }, 1000) // This is a quick hack, but it works
}

console.log("ChatGPT - Review & Rebuttal: active and ready!")
} else {
console.warn("ChatGPT - Review & Rebuttal: not support this site!")
Expand Down
25 changes: 15 additions & 10 deletions src/content-script/interface-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ export interface ElementInterface {
reviewButtonParent: string,
rebuttalButtonParent: string,
resultContainer: string,
resultContainerChild: string
loadingElement: string,
titleElement: string,
abstractElement: string,
}

export const config: Record<string, ElementInterface> = {
notebook: {
reviewButtonParent: ".reply_row",
rebuttalButtonParent: ".reply_row",
resultContainer: '#pager',
resultContainerChild : "#pager-container",

resultContainer: '#content > .forum-container > .note',
loadingElement: "#note_children > .spinner-container",
titleElement: "#content > .forum-container > .note > .title_pdf_row > .note_content_title",
abstractElement: "#content > .forum-container > .note > .note_contents",
},
icml2023: {
reviewButtonParent: ".invitations-container > .invitation-buttons",
rebuttalButtonParent: ".invitations-container > .invitation-buttons",
resultContainer: '#content > .forum-container > .forum-replies-container',
loadingElement: "#forum-replies > .spinner",
titleElement: "#content > div > div.forum-note > div.forum-title.mt-2.mb-2 > h2",
abstractElement: "#content > div > div.forum-note > div.note-content",
},
lab: {
reviewButtonParent: ".reply_row",
rebuttalButtonParent: ".reply_row",
resultContainer: '#labContainerParent',
resultContainerChild : "#labContainerChild",
}
}
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Review & Rebuttal",
"description": "A browser extension for generating reviews and rebuttals, powered by ChatGPT.",
"version": "1.0.0",
"manifest_version": 3,
"version": "1.0.1",
"manifest_version": 4,
"icons": {
"16": "logo.png",
"32": "logo.png",
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.v2.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Review & Rebuttal",
"description": "A browser extension for generating reviews and rebuttals, powered by ChatGPT.",
"version": "1.0.0",
"manifest_version": 2,
"version": "1.0.1",
"manifest_version": 4,
"icons": {
"16": "logo.png",
"32": "logo.png",
Expand Down

0 comments on commit bb9f43b

Please sign in to comment.