- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用二维码来简化我的 Android 智能手机对某些论坛的浏览。
我正在寻找一个 Greasemonkey 脚本,该脚本可以在论坛主题的每个帖子、每个永久链接旁边放置一个二维码。
我有一些可以使用的模板,即 YouTube“分享”QR 脚本:
var shareBoxCheckInterval = setInterval (AddQR_Code, 200);
function AddQR_Code () {
var shareDiv = document.querySelector ('.share-option-container .ytg-box');
if (shareDiv) {
var qrIMG = 'http://chart.googleapis.com/chart?chl='
+ window.location.href + '&chld=M%7C0&cht=qr&chs=125x125';
var img = document.createElement ('img');
img.src = qrIMG;
img.width = 125;
img.height = 125;
shareDiv.appendChild (img);
clearInterval (shareBoxCheckInterval);
}
}
它的作用是将 QR 码添加到 Youtube 的共享框,如下所示:
轻松地将视频从电脑传输到手机。
如何调整此代码以与论坛永久链接一起使用,并使用 QR 代码图像替换链接文本?
例如,在this thread on the Minecraft forum上,每个帖子的右上角都有一个小链接,上面写着“#1”、“#2”、“#3”,无限循环——链接到该特定帖子。
用户脚本将执行的操作是将“#1”文本替换为链接到该帖子的 QR 代码图像(由 Google API 生成),同时也是可点击的超链接图像(也链接到该帖子)。
然后,它会对页面上的每个永久链接重复此操作。
这可能吗?如果可能的话,如何实现?
最佳答案
好的,这是一个完整的脚本,它循环浏览帖子书签并添加二维码。
我留下了帖子号码,因为它们对我使用的论坛很有用。如果您确实希望它们消失,请在 $(this).append (...
行之前添加 $(this).text ("");
。 p>
注意使用 CSS 进行样式设置(好的),而不是标记属性(坏的)。
该脚本稍微复杂一些,需要 withPages_jQuery
结构来使其与 Google Chrome 兼容(如 userscripts 标记所示)。
// ==UserScript==
// @name _Minecraft Forum, post barcodizer
// @namespace _pc
// @include http://www.minecraftforum.net/topic/*
// @grant GM_addStyle
// ==/UserScript==
function GM_scriptMain ($) {
var postBkMarks = $("div.post_block div.post_wrap h3 span.post_id a");
postBkMarks.each ( function () {
var qrIMG = 'http://chart.googleapis.com/chart?chl='
+ encodeURIComponent (this.href)
+ '&chld=M%7C0&cht=qr&chs=125x125'
;
$(this).append ('<img src="' + qrIMG + '">');
} );
}
withPages_jQuery (GM_scriptMain);
GM_addStyle (
"h3 span.post_id a img {width: 125px; height: 125px;}"
);
function withPages_jQuery (NAMED_FunctionToRun) {
//--- Use named functions for clarity and debugging...
var funcText = NAMED_FunctionToRun.toString ();
var funcName = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
var script = document.createElement ("script");
script.textContent = funcText + "\n\n";
script.textContent += 'jQuery(document).ready(function() {'+funcName+'(jQuery);});';
document.body.appendChild (script);
};
(适用于 FF/GM、Chrome、Tampermonkey 和其他浏览器)。
<小时/><小时/>Firefox (Greasemonkey) - only 版本(可能还有 Tampermonkey)更简单:
// ==UserScript==
// @name _Minecraft Forum, post barcodizer
// @namespace _pc
// @include http://www.minecraftforum.net/topic/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_addStyle
// ==/UserScript==
var postBkMarks = $("div.post_block div.post_wrap h3 span.post_id a");
postBkMarks.each ( function () {
var qrIMG = 'http://chart.googleapis.com/chart?chl='
+ encodeURIComponent (this.href)
+ '&chld=M%7C0&cht=qr&chs=125x125'
;
$(this).append ('<img src="' + qrIMG + '">');
} );
GM_addStyle (
"h3 span.post_id a img {width: 125px; height: 125px;}"
);
关于javascript - 如何为论坛中的永久链接生成二维码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231730/
我是一名优秀的程序员,十分优秀!