【问题标题】:Youtube QR Code Generation UserScript not functioningYoutube QR 码生成用户脚本不起作用
【发布时间】:2012-02-25 20:37:41
【问题描述】:

我正在尝试创建一个用户脚本,它会自动将当前网址的二维码图像添加到 youtube 视频页面上的“共享”菜单中。

我对 JavaScript、UserScript、HTML 等几乎一无所知。 但是,这是我目前所拥有的:

// ==UserScript==
// @name           Youtube QR ShareLink 
// @description    Displays QR of youtube URL 
// @version        0.1
// @match          http://www.youtube.com/watch*
// @match          https://www.youtube.com/?*
// @match          http://www.youtube.com/?*
// @match          https://www.youtube.com/watch*
// @include        http://www.youtube.com/?*
// @include        http://www.youtube.com/watch*
// @include        https://www.youtube.com/?*
// @include        https://www.youtube.com/watch*
// ==/UserScript==

(function () {
    var shareDiv = document.getElementById('share-option-container ytg-box');
    var qrIMG = 'http://chart.googleapis.com/chart?chl=' + window.location.href + '&chld=M%7C0&cht=qr&chs=125x125';
    var img = document.createElement('qrcode');
    img.src=qrIMG;
    img.width=125;
    img.height=125;
    shareDiv.appendChild(img);
}());

不出所料,它不起作用。 谁能告诉我我做错了什么?

谢谢!

【问题讨论】:

  • 这里没有意义:'share-option-container ytg-box' id 不能有空格。

标签: javascript youtube share qr-code userscripts


【解决方案1】:

您正在使用 document.getElementById 的值不是框的 id - 它是该元素的类列表。要使用这样的选择器,您可以通过几次调用 document.getElementsByClassName 来实现,或者您可以使用 document.querySelector('.share-option-container .ytg-box'),或者您可以使用 jQuery 来执行该选择。

您的第二个问题是您正在创建一个名为“qrcode”的元素,但您应该创建一个img 元素。

你修改后的代码应该是这样的:

// ==UserScript==
// @name           Youtube QR ShareLink 
// @description    Displays QR of youtube URL 
// @version        0.1
// @match          http://www.youtube.com/watch*
// @match          https://www.youtube.com/?*
// @match          http://www.youtube.com/?*
// @match          https://www.youtube.com/watch*
// @include        http://www.youtube.com/?*
// @include        http://www.youtube.com/watch*
// @include        https://www.youtube.com/?*
// @include        https://www.youtube.com/watch*
// ==/UserScript==

(function () {
    var shareDiv = document.querySelector('.share-option-container .ytg-box');
    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);
}());

请注意,在 YouTube 上,您要抓取的元素在实际打开共享框之前并不存在,因此您需要先实际处理打开共享框,然后才能运行其余代码。我已经在我的浏览器中对此进行了测试,一旦打开共享框,上面的代码就可以正常工作,但之前不行。

您可以使用计时器来解决这个问题。将代码更改为:

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);

        /*-- If you want to continually check for new share boxes, on the
            same page, comment out this next line.
        */
        clearInterval (shareBoxCheckInterval);
    }
}

【讨论】:

  • 感谢您的回复!但请原谅我是一个糟糕的学习者,你究竟是如何打开分享框的? (或者是否可以让脚本在每次打开共享框时执行,而不是强制打开共享框本身?)
  • 您可以运行代码,只有当共享框存在时,通过在计时器内检查它。我冒昧地在这个答案中添加了这个调整。
  • Rofl,你把 youtube 搞砸了! :P 它可以正确显示,但现在它会在每个时间间隔创建一个新图像(最后一行注释掉)我可能应该让它检查图像是否已经存在于共享框中。无论如何,感谢您的帮助:)
  • 哦,我通过取消注释该行来“修复”它。事实证明,它实际上并不是每次都创建一个新的共享框,而是在您关闭它时隐藏/显示它。再次感谢您!
  • 这个脚本非常适合我的 Android :D 是否可以按原样(带学分)发布到 userscripts.org? =3
猜你喜欢
  • 1970-01-01
  • 2017-06-21
  • 2020-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
相关资源
最近更新 更多