|
本帖最后由 coult3 于 2025-6-2 13:55 编辑
按 F1 以论坛格式复制当前网站的标题和网址,这样在本论坛回复链接文本会方便一点。
F1键可能与cent的帮助中心冲突,可以去 chrome://settings/cbManageShortcuts 把 帮助中心 的 F1 停用掉。
或者可以自定义快捷键,替换 if (event.key === 'F1') 就可以。
- // alt+v
- if (event.altKey && event.key === 'v')
- // ctrl+空格
- if (event.ctrlKey && event.key === ' ')
复制代码
ps: 因为chrome的安全策略限制,脚本在 以chrome:// 开头的网址 以及 谷歌扩展商店 不起作用。
- // ==UserScript==
- // @name 快速复制网址和标题(论坛格式)
- // @version 1.2
- // @description 按F1复制当前网站的标题和网址
- // @author You
- // @match *://*/*
- // @grant GM_setClipboard
- // @grant GM_notification
- // ==/UserScript==
- (function() {
- 'use strict';
- // 监听键盘事件
- document.addEventListener('keydown', function(event) {
- // F1键 - 复制标题和URL(论坛格式)
- if (event.key === 'F1') {
- event.preventDefault(); // 阻止默认行为
- const url = window.location.href;
- const title = getTitle(url);
- const cleanUrl = preprocessUrl(url);
- const formattedText = `[url=${cleanUrl}]${title}[/url]`;
- GM_setClipboard(formattedText, 'text');
- showNotification('已复制论坛格式链接', formattedText);
- }
- });
- // 根据URL获取标题
- function getTitle(url) {
- if (url.startsWith('https://www.centbrowser.net/zh-cn/forum.php?mod=viewthread')) {
- const threadSubject = document.querySelector("#thread_subject");
- return threadSubject ? threadSubject.innerText : document.title;
- }
- return document.title;
- }
- // 预处理特定网站的URL,使用正则表达式
- function preprocessUrl(url) {
- // 处理B站URL - 去掉最后一个左斜杠后面的全部字符串
- if (url.startsWith('https://www.bilibili.com')) {
- return url.replace(/\/[^\/]*$/, '');
- // 处理百家号URL - 去掉url中的&及其后面的全部字符串
- } else if (url.startsWith('https://baijiahao.baidu.com')) {
- return url.replace(/&.*$/, '');
- }
- return url;
- }
- // 显示通知
- function showNotification(title, text) {
- GM_notification({
- title: title,
- text: text,
- timeout: 2000
- });
- }
- })();
复制代码
更新了一下,如果是在本论坛的帖子,则获取简短的标题。
一个简单的脚本,祝大家端午六一快乐!
|
|