Fix the bug that occasionally the button does not appear
This commit is contained in:
parent
ec19496e31
commit
8b27917188
23
jetbra.js
23
jetbra.js
|
@ -1,11 +1,11 @@
|
||||||
// ==UserScript==
|
// ==UserScript==
|
||||||
// @name JetBra
|
// @name JetBra
|
||||||
// @namespace https://github.com/novice88/jetbra
|
// @namespace https://github.com/novice88/jetbra
|
||||||
// @version 3.0
|
// @version 3.1
|
||||||
// @license MIT
|
// @license MIT
|
||||||
// @description Add a button on the plugin homepage and click to get the plugin activation code
|
// @description Add a button on the plugin homepage and click to get the plugin activation code
|
||||||
// @author novice.li
|
// @author novice.li
|
||||||
// @match https://plugins.jetbrains.com/plugin/*
|
// @match https://plugins.jetbrains.com/*
|
||||||
// @grant GM_setClipboard
|
// @grant GM_setClipboard
|
||||||
// @grant GM_addStyle
|
// @grant GM_addStyle
|
||||||
// @grant GM_xmlhttpRequest
|
// @grant GM_xmlhttpRequest
|
||||||
|
@ -29,6 +29,7 @@ async function findElementWithRetry(cssSelector) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let addButton = async function () {
|
let addButton = async function () {
|
||||||
|
console.log('JetBra is running');
|
||||||
'use strict';
|
'use strict';
|
||||||
GM_addStyle(`
|
GM_addStyle(`
|
||||||
.jetbra-button {
|
.jetbra-button {
|
||||||
|
@ -50,11 +51,16 @@ let addButton = async function () {
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
const backendBaseUrl = 'https://jetbra.noviceli.win'
|
const backendBaseUrl = 'https://jetbra.noviceli.win'
|
||||||
const metaTag = document.querySelector('meta[name="pluginId"]')
|
|
||||||
if (!metaTag) {
|
// 获取插件的 id
|
||||||
return
|
// 如果当前url满足 https://plugins.jetbrains.com/plugin/<pluginId>-xxx 的格式,就直接从url中获取
|
||||||
|
let url = window.location.href
|
||||||
|
if (!url.startsWith('https://plugins.jetbrains.com/plugin/')) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const pluginId = metaTag.getAttribute('content')
|
// 提取 pluginId
|
||||||
|
let pluginId = url.split('/')[4].split('-')[0]
|
||||||
|
console.log('pluginId: ' + pluginId);
|
||||||
|
|
||||||
let pluginDetail = await fetch('https://plugins.jetbrains.com/api/plugins/' + pluginId).then(r => r.json());
|
let pluginDetail = await fetch('https://plugins.jetbrains.com/api/plugins/' + pluginId).then(r => r.json());
|
||||||
|
|
||||||
|
@ -107,9 +113,8 @@ let addButton = async function () {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
window.onload = function () {
|
|
||||||
addButton();
|
addButton();
|
||||||
}
|
|
||||||
if (window.onurlchange === null) {
|
if (window.onurlchange === null) {
|
||||||
window.addEventListener('urlchange', (info) => {
|
window.addEventListener('urlchange', (info) => {
|
||||||
addButton();
|
addButton();
|
||||||
|
|
Loading…
Reference in New Issue