Skip to content

Commit

Permalink
Merge pull request #4 from LiLittleCat/2-add-a-refresh-or-a-reload-bu…
Browse files Browse the repository at this point in the history
…tton

fix #2, remove session token check
  • Loading branch information
LiLittleCat authored Dec 16, 2022
2 parents e9e6d06 + 90034fd commit 3cea3d3
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# intellij-chatgpt Changelog

## [Unreleased]
- Remove session token check because of Cloudflare
- Add refresh button [#2](https://github.com/LiLittleCat/intellij-chatgpt/issues/2)
- Support version 2020.2-2022.3 [#3](https://github.com/LiLittleCat/intellij-chatgpt/issues/3)

## [1.0.1] - 2022-12-12

Expand Down
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import org.jetbrains.changelog.Changelog
import org.jetbrains.changelog.markdownToHTML
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.ir.backend.js.compile

fun properties(key: String) = project.findProperty(key).toString()

Expand All @@ -11,7 +10,7 @@ plugins {
// Kotlin support
id("org.jetbrains.kotlin.jvm") version "1.7.21"
// Gradle IntelliJ Plugin
id("org.jetbrains.intellij") version "1.10.0"
id("org.jetbrains.intellij") version "1.10.1"
// Gradle Changelog Plugin
id("org.jetbrains.changelog") version "2.0.0"
// Gradle Qodana Plugin
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pluginGroup = com.lilittlecat.intellij-chatgpt
pluginName = ChatGPT Tool
pluginRepositoryUrl = https://github.com/LiLittleCat/intellij-chatgpt
# SemVer format -> https://semver.org
pluginVersion = 1.0.1
pluginVersion = 1.0.2

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 221
pluginSinceBuild = 202
pluginUntilBuild = 223.*

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType = IC
platformVersion = 2022.1
platformVersion = 2020.2

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/lilittlecat/chatgpt/action/RefreshAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.lilittlecat.chatgpt.action;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
import com.lilittlecat.chatgpt.message.ChatGPTBundle;
import com.lilittlecat.chatgpt.window.ChatGPTToolWindow;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* @author LiLittleCat
* @link <a href="https://github.com/LiLittleCat">https://github.com/LiLittleCat</a>
* @since 2022/12/15
*/
public class RefreshAction extends DumbAwareAction {
public RefreshAction(@NotNull @Nls String text) {
super(() -> text, AllIcons.Actions.Refresh);
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
ToolWindowManager instance = ToolWindowManager.getInstance(Objects.requireNonNull(e.getProject()));
ToolWindow toolWindow = instance.getToolWindow("ChatGPT");
if (toolWindow != null) {
ContentManager contentManager = toolWindow.getContentManager();
Content browserContent = contentManager.findContent(ChatGPTBundle.message("browser.tab.name"));
if (browserContent != null) {
contentManager.removeContent(browserContent, false);
}
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content browser = contentFactory.createContent(new ChatGPTToolWindow().getContent(),
ChatGPTBundle.message("browser.tab.name"), false);
contentManager.addContent(browser);
contentManager.setSelectedContent(browser);
}
}
}
51 changes: 27 additions & 24 deletions src/main/java/com/lilittlecat/chatgpt/window/ChatGPTToolWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.lilittlecat.chatgpt.setting.ChatGPTSettingsState;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -59,25 +58,27 @@ public ChatGPTToolWindow() {
if (StringUtils.isNotBlank(sessionToken)) {
// check the token is right or not

String cookie = sessionTokenName + "=" + sessionToken;
HttpGet httpGet = new HttpGet("https://chat.openai.com/chat");
httpGet.setHeader("Connection", "keep-alive");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15");
httpGet.setHeader("Cookie", cookie);
httpGet.setHeader("Origin", "https://auth0.openai.com");
httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpGet.setHeader("Accept-Language", "en-US,en;q=0.9");
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
HttpEntity httpEntity = httpResponse.getEntity();
String bodyString = EntityUtils.toString(httpEntity);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK && !bodyString.contains("Welcome to ChatGPT")) {
JBCefCookie jbCefCookie = new JBCefCookie(sessionTokenName, sessionToken, "chat.openai.com", "/", true, true);
jbCefCookieManager.setCookie("https://chat.openai.com", jbCefCookie);
}
} catch (IOException e) {
LOG.error("Error when check session token: ", e);
}
// 2022.12.16 with Cloudflare, can not check token by http get

// String cookie = sessionTokenName + "=" + sessionToken;
// HttpGet httpGet = new HttpGet("https://chat.openai.com/chat");
// httpGet.setHeader("Connection", "keep-alive");
// httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15");
// httpGet.setHeader("Cookie", cookie);
// httpGet.setHeader("Origin", "https://auth0.openai.com");
// httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// httpGet.setHeader("Accept-Language", "en-US,en;q=0.9");
// try (CloseableHttpClient httpClient = HttpClients.createDefault();
// CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
// HttpEntity httpEntity = httpResponse.getEntity();
// String bodyString = EntityUtils.toString(httpEntity);
// if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK && !bodyString.contains("Welcome to ChatGPT")) {
JBCefCookie jbCefCookie = new JBCefCookie(sessionTokenName, sessionToken, "chat.openai.com", "/", true, true);
jbCefCookieManager.setCookie("https://chat.openai.com", jbCefCookie, null);
// }
// } catch (IOException e) {
// LOG.error("Error when check session token: ", e);
// }
}
}
// get session token after login, fill it in settings
Expand All @@ -86,10 +87,12 @@ public ChatGPTToolWindow() {
String currentSessionToken = null;
while (currentSessionToken == null) {
List<JBCefCookie> cookies = jbCefCookieManager.getCookies();
for (JBCefCookie cookie : cookies) {
if (cookie.getName().equals(sessionTokenName)) {
currentSessionToken = cookie.getValue();
ChatGPTSettingsState.getInstance().update(currentSessionToken);
if (!cookies.isEmpty()) {
for (JBCefCookie cookie : cookies) {
if (cookie.getName().equals(sessionTokenName)) {
currentSessionToken = cookie.getValue();
ChatGPTSettingsState.getInstance().update(currentSessionToken);
}
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.lilittlecat.chatgpt.action.RefreshAction;
import com.lilittlecat.chatgpt.action.SettingsAction;
import com.lilittlecat.chatgpt.message.ChatGPTBundle;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand All @@ -22,11 +24,12 @@ public class ChatGPTToolWindowFactory implements ToolWindowFactory {
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
ContentManager contentManager = toolWindow.getContentManager();
Content labelContent = contentManager.getFactory().createContent(
new ChatGPTToolWindow().getContent(), null, false);
new ChatGPTToolWindow().getContent(), ChatGPTBundle.message("browser.tab.name"), false);
contentManager.addContent(labelContent);
// add actions to tool window
List<AnAction> anActionList = new ArrayList<>();
anActionList.add(new SettingsAction("Settings"));
anActionList.add(new RefreshAction("Refresh"));
toolWindow.setTitleActions(anActionList);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/messages/ChatGPTBundle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name=ChatGPT

setting.session.token.label=Session token:
setting.menu.text=ChatGPT
setting.menu.text=ChatGPT
browser.tab.name=chat.openai.com

0 comments on commit 3cea3d3

Please sign in to comment.