找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 283|回复: 2

一个简单的脚本,祝大家端午六一快乐!

[复制链接]

45

主题

409

回帖

2548

积分

金牌会员

积分
2548
发表于 6 天前 | 显示全部楼层 |阅读模式
本帖最后由 coult3 于 2025-6-2 13:55 编辑

按 F1 以论坛格式复制当前网站的标题和网址,这样在本论坛回复链接文本会方便一点。
F1键可能与cent的帮助中心冲突,可以去 chrome://settings/cbManageShortcuts 把 帮助中心 的 F1 停用掉。
或者可以自定义快捷键,替换 if (event.key === 'F1') 就可以。
  1. // alt+v
  2. if (event.altKey && event.key === 'v')
  3. // ctrl+空格
  4. if (event.ctrlKey && event.key === ' ')
复制代码

ps: 因为chrome的安全策略限制,脚本在 以chrome:// 开头的网址 以及 谷歌扩展商店  不起作用。

  1. // ==UserScript==
  2. // @name         快速复制网址和标题(论坛格式)
  3. // @version      1.2
  4. // @description  按F1复制当前网站的标题和网址
  5. // @author       You
  6. // @match        *://*/*
  7. // @grant        GM_setClipboard
  8. // @grant        GM_notification
  9. // ==/UserScript==

  10. (function() {
  11.     'use strict';

  12.     // 监听键盘事件
  13.     document.addEventListener('keydown', function(event) {
  14.         // F1键 - 复制标题和URL(论坛格式)
  15.         if (event.key === 'F1') {
  16.             event.preventDefault(); // 阻止默认行为
  17.             const url = window.location.href;
  18.             const title = getTitle(url);
  19.             const cleanUrl = preprocessUrl(url);
  20.             const formattedText = `[url=${cleanUrl}]${title}[/url]`;
  21.             GM_setClipboard(formattedText, 'text');
  22.             showNotification('已复制论坛格式链接', formattedText);
  23.         }
  24.     });

  25.     // 根据URL获取标题
  26.     function getTitle(url) {
  27.         if (url.startsWith('https://www.centbrowser.net/zh-cn/forum.php?mod=viewthread')) {
  28.             const threadSubject = document.querySelector("#thread_subject");
  29.             return threadSubject ? threadSubject.innerText : document.title;
  30.         }
  31.         return document.title;
  32.     }

  33.     // 预处理特定网站的URL,使用正则表达式
  34.     function preprocessUrl(url) {

  35.           // 处理B站URL - 去掉最后一个左斜杠后面的全部字符串
  36.         if (url.startsWith('https://www.bilibili.com')) {
  37.             return url.replace(/\/[^\/]*$/, '');
  38.           // 处理百家号URL - 去掉url中的&及其后面的全部字符串
  39.         } else if (url.startsWith('https://baijiahao.baidu.com')) {
  40.             return url.replace(/&.*$/, '');
  41.         }

  42.         return url;

  43.     }

  44.     // 显示通知
  45.     function showNotification(title, text) {
  46.         GM_notification({
  47.             title: title,
  48.             text: text,
  49.             timeout: 2000
  50.         });
  51.     }
  52. })();
复制代码


更新了一下,如果是在本论坛的帖子,则获取简短的标题。


一个简单的脚本,祝大家端午六一快乐!





回复

使用道具 举报

45

主题

409

回帖

2548

积分

金牌会员

积分
2548
 楼主| 发表于 6 天前 | 显示全部楼层
回复

使用道具 举报

9

主题

8124

回帖

2万

积分

超级版主

(^^ゞ

积分
22784
发表于 6 天前 | 显示全部楼层
感谢分享!
¯\(°_o)/¯
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|百分浏览器论坛

GMT+8, 2025-6-7 21:14 , Processed in 0.016284 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表