Skip to content

Commit

Permalink
Bring back session token (#22)
Browse files Browse the repository at this point in the history
* feat: bring session token setting back

* feat: change log

* feat: update README

* feat: update README

* feat: update description
  • Loading branch information
LiLittleCat authored Mar 30, 2023
1 parent 04a1cbc commit eb8c97f
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 77 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## [Unreleased]

### Added
- Bring session token setting for official website back. [#7 (comment)](https://github.com/LiLittleCat/intellij-chatgpt/issues/7#issuecomment-1487889326)

## [1.1.0] - 2023-03-21

### Added
Expand Down
12 changes: 12 additions & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ providing a seamless experience for developers to interact with the ChatGPT AI m
This plugin uses JCEF(Java Chromium Embedded Framework) to render the content. It is a lightweight and cross-platform web browser engine that is built on top of Chromium and is used by IntelliJ IDEA to render the HTML content of the IDE.
JCEF is supported in IntelliJ IDEA 2020.2 and later. See [JCEF](https://plugins.jetbrains.com/docs/intellij/jcef.html) for more information.

### 💡 Solution for `Cannot log in to the official ChatGPT by Google account or Microsoft account`

See [Notice from README](https://github.com/LiLittleCat/intellij-chatgpt/blob/bring-back-session-token/README.md#-notice).

### Support

Feel free to submit [issue](https://github.com/LiLittleCat/awesome-free-chatgpt/issues) and [PRs](https://github.com/LiLittleCat/awesome-free-chatgpt/pulls). If you find it useful, welcome to star ⭐, your support is my motivation to move forward.

---
Expand All @@ -34,6 +40,12 @@ Feel free to submit [issue](https://github.com/LiLittleCat/awesome-free-chatgpt/
此插件使用 JCEF(Java Chromium Embedded Framework)来渲染内容。它是一个轻量级且跨平台的基于 Chromium 的网络浏览器引擎,被 IntelliJ IDEA 用于渲染 IDE 的 HTML 内容。
JCEF 支持 IntelliJ IDEA 2020.2 及更高版本。有关更多信息,请参阅 [JCEF](https://plugins.jetbrains.com/docs/intellij/jcef.html)

### 💡 `无法用谷歌账户或微软账户登录官方 ChatGPT` 的解决方案

[README 中的注意](https://github.com/LiLittleCat/intellij-chatgpt/blob/bring-back-session-token/README_CN.md#-%E6%B3%A8%E6%84%8F)

### 支持

欢迎随时提交 [issue](https://github.com/LiLittleCat/awesome-free-chatgpt/issues)[PRs](https://github.com/LiLittleCat/awesome-free-chatgpt/pulls),如果您觉得有用,欢迎 star ⭐,您的支持是我前进的动力。

<!-- Plugin description end -->
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@
This Jetbrains IDEs plugin integrates [ChatGPT](https://chat.openai.com/) and [other third-party mirror websites](https://github.com/LiLittleCat/awesome-free-chatgpt) of ChatGPT into JetBrains IDEs,
providing a seamless experience for developers to interact with the ChatGPT AI model directly within their development environment.

> ### 💡 Notice
> Cannot log in to the official ChatGPT by Google account or Microsoft account is not caused by this plugin, but by the
> official ChatGPT itself.
>
> See https://github.com/LiLittleCat/intellij-chatgpt/issues/7 and https://github.com/JetBrains/jcef/issues/14 for more
> information.
>
> <details>
> <summary>💡 Click to expand the Solution for this</summary>
>
> 1. Go to [https://chat.openai.com/chat](https://chat.openai.com/chat) and log in or sign up.
> 2. Open dev tools.
> 3. Open `Application` > `Cookies`.
[![pSSKdmR.png](https://s1.ax1x.com/2022/12/28/pSSKdmR.png)](https://imgse.com/i/pSSKdmR)
> 4. Copy the value for `__Secure-next-auth.session-token` as settings value.
[![pSSK6pD.png](https://s1.ax1x.com/2022/12/28/pSSK6pD.png)](https://imgse.com/i/pSSK6pD)
> 5. After you enter the session token, you need to restart the IDE because ChatGPT Tool Windows needs a restart, and
you won't need to log in until the session token is expired.
> </details>
## Feature

- 🚀 Easy to use, and interact with ChatGPT without leaving the IDE.
Expand Down Expand Up @@ -58,11 +78,4 @@ JCEF is supported in IntelliJ IDEA 2020.2 and later. See [JCEF](https://plugins.
9. Delete the selected URL.
10. Fetch the URL list from [the author's other GitHub repository](https://github.com/LiLittleCat/awesome-free-chatgpt).
4. Utilize third-party mirror websites.
![](/image/another.png)


> **Notice**
>
> Cannot log in to the official ChatGPT by Google account or Microsoft account is not caused by this plugin, but by the official ChatGPT itself.
>
> See https://github.com/LiLittleCat/intellij-chatgpt/issues/7 and https://github.com/JetBrains/jcef/issues/14 for more information.
![](/image/another.png)
25 changes: 18 additions & 7 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

一个将 [ChatGPT](https://chat.openai.com/)[其他第三方镜像网站](https://github.com/LiLittleCat/awesome-free-chatgpt) 整合到 JetBrains IDEs 的插件。

> ### 💡 注意
> 无法使用 Google 账户或 Microsoft 账户登录官方 ChatGPT 不是由于此插件造成的,而是由官方 ChatGPT 本身造成的。
>
> 有关更多信息,请参阅 https://github.com/LiLittleCat/intellij-chatgpt/issues/7https://github.com/JetBrains/jcef/issues/14
>
> <details>
> <summary>💡 点击展开解决方案</summary>
>
> 1. 进入 [https://chat.openai.com/chat](https://chat.openai.com/chat),登录或注册。
> 2. 打开开发工具。
> 3. 打开 `Application` > `Cookies`
[![pSSKdmR.png](https://s1.ax1x.com/2022/12/28/pSSKdmR.png)](https://imgse.com/i/pSSKdmR)
> 4. 复制 `__Secure-next-auth.session-token` 的值作为设置值。
[![pSSK6pD.png](https://s1.ax1x.com/2022/12/28/pSSK6pD.png)](https://imgse.com/i/pSSK6pD)
> 5. 输入会话令牌后,你需要重启 IDE,因为 ChatGPT 工具 Windows 需要重启,在会话令牌过期之前,你不需要登录。
> </details>
## 功能

- 🚀 易于使用,无需离开 IDE 即可与 ChatGPT 交互。
Expand Down Expand Up @@ -56,10 +73,4 @@ JCEF 支持 IntelliJ IDEA 2020.2 及更高版本。有关更多信息,请参
9. 删除选定的 URL。
10.[作者的另一个 GitHub 仓库](https://github.com/LiLittleCat/awesome-free-chatgpt) 获取 URL 列表。
4. 使用第三方镜像网站。
![](/image/another.png)

> **注意**
>
> 无法使用 Google 账户或 Microsoft 账户登录官方 ChatGPT 不是由于此插件造成的,而是由官方 ChatGPT 本身造成的。
>
> 有关更多信息,请参阅 https://github.com/LiLittleCat/intellij-chatgpt/issues/7https://github.com/JetBrains/jcef/issues/14
![](/image/another.png)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.lilittlecat.intellij-chatgpt
pluginName = ChatGPT Tool
pluginRepositoryUrl = https://github.com/LiLittleCat/intellij-chatgpt
# SemVer format -> https://semver.org
pluginVersion = 1.1.0
pluginVersion = 1.1.1

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 202
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
// Store the index of the selected content
int selectedIndex = contentManager.getIndexOfContent(selectedContent);
// Remove the selected content
contentManager.removeContent(selectedContent, false);
contentManager.removeContent(selectedContent, true);
// Iterate through all contents in the content manager
for (int i = 0; i < contentManager.getContentCount(); i++) {
// Get the current content
Expand All @@ -51,7 +51,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
// Get the tab name of the current content
String tabName = content.getTabName();
// Remove the current content
contentManager.removeContent(content, false);
contentManager.removeContent(content, true);
// Create a new content with the same tab name
Content browser = ContentFactory.SERVICE.getInstance().createContent(
new ChatGPTToolWindow(tabName).getContent(), tabName, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.*;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTextArea;
import com.intellij.ui.components.fields.ExtendableTextField;
import com.intellij.ui.table.JBTable;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.JBInsets;
import com.intellij.util.ui.ListTableModel;
import com.intellij.util.ui.*;
import com.lilittlecat.chatgpt.action.FetchURLAction;
import com.lilittlecat.chatgpt.message.ChatGPTBundle;
import org.jetbrains.annotations.Nls;
Expand Down Expand Up @@ -84,6 +83,7 @@ public void addUrl(@NotNull String url) {
private Disposable myDisposable = Disposer.newDisposable();

private JPanel myMainPanel;
private JBTextArea sessionToken = new JBTextArea();
private JComboBox<String> defaultUrlComboBox;

private ListTableModel<String> myModel;
Expand Down Expand Up @@ -140,7 +140,14 @@ public void editingCanceled(ChangeEvent e) {
JComponent table = createTable();
table.setPreferredSize(new Dimension(500, 200));
createComboBox();

sessionToken.setFont(UIUtil.getLabelFont());
sessionToken.setLineWrap(true);
JBScrollPane scrollPane = new JBScrollPane(sessionToken,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
myMainPanel = FormBuilder.createFormBuilder()
.addLabeledComponent(new JBLabel(ChatGPTBundle.message("setting.session.token.label")), scrollPane, 1, true)
.addLabeledComponent(new JBLabel(ChatGPTBundle.message("default.url.message")), defaultUrlComboBox, 1, false)
.addLabeledComponent(new SeparatorComponent(), new JPanel(), 1, false)
.addLabeledComponent(new JBLabel(ChatGPTBundle.message("url.list.message")), table, 3, true)
Expand Down Expand Up @@ -261,6 +268,7 @@ public boolean isModified() {
List<String> items = myModel.getItems();
modified |= urlList.size() != items.size();
modified |= !urlList.equals(items);
modified |= !instance.sessionToken.equals(sessionToken.getText());
return modified;
}

Expand All @@ -270,6 +278,7 @@ public void apply() {
ChatGPTSettingsState instance = ChatGPTSettingsState.getInstance();
instance.defaultUrl = Objects.requireNonNull(defaultUrlComboBox.getSelectedItem()).toString();
instance.urlList = myModel.getItems();
instance.sessionToken = sessionToken.getText();
// Refresh the UI after updating the settings
refreshUI();
// instance.urlList.addAll(myModel.getItems());
Expand All @@ -284,5 +293,6 @@ public void reset() {
defaultUrlComboBox.addItem(s);
}
defaultUrlComboBox.setSelectedItem(instance.defaultUrl);
sessionToken.setText(instance.sessionToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
storages = @Storage("ChatGPTSettingsState.xml")
)
public class ChatGPTSettingsState implements PersistentStateComponent<ChatGPTSettingsState> {
// public String sessionToken = "";

public String sessionToken = "";

public String defaultUrl = ChatGPTBundle.message("default.url");

Expand Down Expand Up @@ -68,6 +69,14 @@ public void setUrlList(List<String> urlList) {
this.urlList = urlList;
}

public String getSessionToken() {
return sessionToken;
}

public void setSessionToken(String sessionToken) {
this.sessionToken = sessionToken;
}

public static ChatGPTSettingsState getInstance() {
return ApplicationManager.getApplication().getService(ChatGPTSettingsState.class);
}
Expand Down
111 changes: 58 additions & 53 deletions src/main/java/com/lilittlecat/chatgpt/window/ChatGPTToolWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,61 +61,66 @@ public ChatGPTToolWindow(String url) {
}
JBCefBrowser jbCefBrowser = new JBCefBrowser();
this.content.add(jbCefBrowser.getComponent(), BorderLayout.CENTER);
// JBCefCookieManager jbCefCookieManager = new JBCefCookieManager();
// if (url.equals(ChatGPTBundle.message("default.url"))) {
// // find if there is session token in settings
// ChatGPTSettingsState state = ChatGPTSettingsState.getInstance().getState();
// if (state != null) {
// String sessionToken = state.sessionToken;
// if (StringUtils.isNotBlank(sessionToken)) {
// // check the token is right or not
//
// // 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, 10000);
//// }
//// } catch (IOException e) {
//// LOG.error("Error when check session token: ", e);
//// }
// }
// }
// // get session token after login, fill it in settings
// // fill it every login, in case invalid session token cause can't login problem
// executor.execute(() -> {
// String currentSessionToken = null;
// while (currentSessionToken == null) {
// List<JBCefCookie> cookies = jbCefCookieManager.getCookies();
// if (!cookies.isEmpty()) {
// for (JBCefCookie cookie : cookies) {
// if (cookie.getName().equals(sessionTokenName)) {
// currentSessionToken = cookie.getValue();
// ChatGPTSettingsState.getInstance().setSessionToken(currentSessionToken);
// }
// }
// }
// try {
// Thread.sleep(1000L);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
if (url.equals(ChatGPTBundle.message("default.url"))) {
setCookieForOfficalWebsite(url);
}
jbCefBrowser.loadURL(url);
}

private void setCookieForOfficalWebsite(String url) {
JBCefCookieManager jbCefCookieManager = new JBCefCookieManager();
// find if there is session token in settings
ChatGPTSettingsState state = ChatGPTSettingsState.getInstance().getState();
if (state != null) {
String sessionToken = state.sessionToken;
if (StringUtils.isNotBlank(sessionToken)) {
// check the token is right or not

// 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);
// use async set
jbCefCookieManager.setCookie(ChatGPTBundle.message("default.url"), jbCefCookie, false);
// }
// } catch (IOException e) {
// LOG.error("Error when check session token: ", e);
// }
// });
// }
jbCefBrowser.loadURL(url);
}
}
// get session token after login, fill it in settings
// fill it every login, in case invalid session token cause can't login problem
executor.execute(() -> {
String currentSessionToken = null;
while (currentSessionToken == null) {
List<JBCefCookie> cookies = jbCefCookieManager.getCookies();
if (!cookies.isEmpty()) {
for (JBCefCookie cookie : cookies) {
if (cookie.getName().equals(sessionTokenName)) {
currentSessionToken = cookie.getValue();
ChatGPTSettingsState.getInstance().setSessionToken(currentSessionToken);
}
}
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}

public JPanel getContent() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/ChatGPTBundle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ChatGPT Tool
setting.session.token.label=Session token:
setting.session.token.label=Session token(only for https://chat.openai.com):
setting.menu.text=ChatGPT Tool
default.url.message=Default URL:
url.list.message=URL list:
Expand Down

0 comments on commit eb8c97f

Please sign in to comment.