Skip to content

Instantly share code, notes, and snippets.

@hoist1999
Last active December 20, 2023 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoist1999/c12db5dae8b3ec0b4373438f29115226 to your computer and use it in GitHub Desktop.
Save hoist1999/c12db5dae8b3ec0b4373438f29115226 to your computer and use it in GitHub Desktop.
// 代码修改后保存到下面网址
// https://gist.github.com/hoist1999/c12db5dae8b3ec0b4373438f29115226/edit
/** 所有插件通用工具类 */
class PluginUtils {
static marketplaceList = [
{
country: "Brazil",
countryCode: "BR",
FBARegion: "BR",
marketplaceId: "A2Q3Y263D00KWC"
},
{
country: "Canada",
countryCode: "CA",
FBARegion: "CA",
marketplaceId: "A2EUQ1WTGCTBG2"
},
{
country: "Mexico",
countryCode: "MX",
FBARegion: "MX",
marketplaceId: "A1AM78C64UM0Y8"
},
{
country: "United States",
countryCode: "US",
FBARegion: "US",
marketplaceId: "ATVPDKIKX0DER"
},
{
country: "United Arab Emirates (U.A.E.)",
countryCode: "AE",
FBARegion: "AE",
marketplaceId: "A2VIGQ35RCS4UG"
},
{
country: "Germany",
countryCode: "DE",
FBARegion: "PanEuro",
marketplaceId: "A1PA6795UKMFR9"
},
{
country: "Egypt",
countryCode: "EG",
FBARegion: "EG",
marketplaceId: "ARBP9OOSHTCHU"
},
{
country: "Spain",
countryCode: "ES",
FBARegion: "PanEuro",
marketplaceId: "A1RKKUPIHCS9HS"
},
{
country: "France",
countryCode: "FR",
FBARegion: "PanEuro",
marketplaceId: "A13V1IB3VIYZZH"
},
{
country: "Belgium",
countryCode: "BE",
FBARegion: "PanEuro",
marketplaceId: "AMEN7PMS3EDWL"
},
{
country: "United Kingdom",
countryCode: "GB",
FBARegion: "UK",
marketplaceId: "A1F83G8C2ARO7P"
},
{
country: "India",
countryCode: "IN",
FBARegion: "IN",
marketplaceId: "A21TJRUUN4KGV"
},
{
country: "Italy",
countryCode: "IT",
FBARegion: "PanEuro",
marketplaceId: "APJ6JRA9NG5V4"
},
{
country: "Netherlands",
countryCode: "NL",
FBARegion: "PanEuro",
marketplaceId: "A1805IZSGTT6HS"
},
{
country: "Poland",
countryCode: "PL",
FBARegion: "PanEuro",
marketplaceId: "A1C3SOZRARQ6R3"
},
{
country: "Saudi Arabia",
countryCode: "SA",
FBARegion: "SA",
marketplaceId: "A17E79C6D8DWNP"
},
{
country: "Sweden",
countryCode: "SE",
FBARegion: "PanEuro",
marketplaceId: "A2NODRKZP88ZB9"
},
{
country: "Turkey",
countryCode: "TR",
FBARegion: "PanEuro",
marketplaceId: "A33AVAJ2PDY3EV"
},
{
country: "Singapore",
countryCode: "SG",
FBARegion: "SG",
marketplaceId: "A19VAU5U5O7RUS"
},
{
country: "Australia",
countryCode: "AU",
FBARegion: "AU",
marketplaceId: "A39IBJ37TRP1C6"
},
{
country: "Japan",
countryCode: "JP",
FBARegion: "JP",
marketplaceId: "A1VC38T7YXB528"
}
];
// 通用函数:传入html文本,转为HtmlElement对象
static htmlToElement(html) {
console.log("hello world 112.");
var template = document.createElement("template");
html = html.trim();
template.innerHTML = html;
return template.content.firstChild;
}
/** 通用函数:等待一个目标元素的出现 */
static waitForElementAsync(selector, timeout = 3000) {
return new Promise((resolve, reject) => {
const targetNode = document.querySelector(selector);
// 目标元素已存在,直接 resolve
if (targetNode) {
resolve(targetNode);
return;
}
// 超时时间到了仍未找到目标元素,reject
const timeoutId = setTimeout(() => {
observer.disconnect(); // 停止监听
reject(
new Error(
`Timeout (${timeout}ms) waiting for element with selector "${selector}"`
)
);
}, timeout);
// 使用 MutationObserver 监听目标元素的出现
const observer = new MutationObserver(() => {
const targetNode = document.querySelector(selector);
if (targetNode) {
observer.disconnect(); // 停止监听
clearTimeout(timeoutId); // 清除超时计时器
resolve(targetNode); // 找到目标元素,resolve
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
});
} // END of waitForElementAsync
/** 等待多少毫秒 */
static sleepAsync(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/** 根据textContent在node的所有子节点中查找对应的节点 */
static findNodeByContent(node, textContent) {
if (node.textContent === textContent) {
return node;
}
for (let i = 0; i < node.childNodes.length; i++) {
const childNode = node.childNodes[i];
const foundNode = Util.findNodeByContent(childNode, textContent);
if (foundNode) {
return foundNode;
}
}
return null;
} // END: findNodeByContent
/** 从文本中取出第一个数 */
static extractFirstNumber(text) {
const regex = /\d+(\.\d+)?/;
const match = text.match(regex);
return match ? Number(match[0]) : null;
}
static addStyle(styleText) {
// 添加样式
const styleSheet = document.createElement("style");
styleSheet.innerHTML = styleText;
// 将样式表添加到页面
document.head.appendChild(styleSheet);
}
/**
* 返回当前日期字符串
* 例如:2023-08-30 18:22:31
*/
static getCurrentDateTimeString() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, "0");
const day = String(now.getDate()).padStart(2, "0");
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
const seconds = String(now.getSeconds()).padStart(2, "0");
const dateTimeString = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return dateTimeString;
}
static getCountryCodeByMarketplaceId(marketplaceId) {
const marketplace = this.marketplaceList.find(item => item.marketplaceId === marketplaceId);
if (marketplace) {
return marketplace.countryCode;
}
return null; // 如果未找到对应的marketplaceId,则返回null或其他适当的值
}
static getFBARegionByMarketplaceId(marketplaceId) {
const marketplace = this.marketplaceList.find(item => item.marketplaceId === marketplaceId);
if (marketplace) {
return marketplace.FBARegion;
}
return null; // 如果未找到对应的marketplaceId,则返回null或其他适当的值
}
} // END Util
/** 广告插件通用工具类 */
class CampaignPluginUtil {
// 向页面左下方添加一个按钮容器
static addButtonDocker() {
var buttonDocker = document.getElementById('button_docker');
if (!buttonDocker) {
// Create the button docker
buttonDocker = PluginUtils.htmlToElement('<div id="button_docker"></div>');
document.body.appendChild(buttonDocker);
// Apply CSS styles to the button docker
GM_addStyle(`
#button_docker {
position: fixed;
max-width: 300px;
bottom: 10px;
left: 60px;
display: flex;
flex-wrap: wrap;
}
#button_docker button {
margin: 5px;
}
`);
}
return buttonDocker;
}
static createBtn(text) {
// 创建一个按钮元素
const button = document.createElement("button");
// 添加样式
button.innerHTML = text;
button.style.backgroundColor = "#E5E6E8";
button.style.marginLeft = "5px";
button.style.border = "none";
button.style.outline = "none";
button.style.cursor = "pointer";
button.style.color = "rgb(35, 47, 63)";
button.style.padding = "6px 8px";
button.style.fontSize = "14px";
button.style.cursor = "pointer";
button.style.zIndex = "9999";
return button;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment