简体   繁体   English

为什么以下jquery代码在tampermonkey中不起作用

[英]why the following jquery code does not work in tampermonkey

I have the following snippet of code to be executed by Tampermonkey in chrome on the website http://www.thefreedictionary.com/abnormally 我在网站http://www.thefreedictionary.com/abnormally上有以下代码段,这些代码将由Tampermonkey以chrome格式执行

I tried it standalone in the page and it works but it does not work through Tampermonkey. 我在页面中单独尝试了它,但是它可以通过Tampermonkey无法工作。

The code is as follows: 代码如下:

// ==UserScript==
// @name       linkify span href
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      http://www.thefreedictionary.com/*
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js
// @copyright  2012+, You
// @grant      GM_log
// ==/UserScript==
//alert("hello");

jQuery(document).ready (Greasemonkey_main);

function Greasemonkey_main ()
{  
   alert("hjello");

   jQuery("span.hvr").each(function () { jQuery(this).wrap('<a href="/' + jQuery(this).text() + '" style="text-decoration:none;color:inherit" target="_blank"></a>')})
}

To check I also put an alert, the alert works, but not the jquery one line of code . 要检查我是否还发出警报,该警报有效,但jquery的一行代码无效。

No clue why . 不知道为什么。 please help . 请帮忙 。

Note : 注意 :

What this one line code does ? 此一行代码有什么作用?

It just wraps every span tag in the page with an anchor tag . 它只是用锚标记包装页面中的每个span标记。

How to try out that code in that page ? 如何在该页面中尝试该代码?

add the script tag of jQuery link and then execute that one line from console and it will work . 添加jQuery链接的脚本标签,然后从控制台执行一行,它将会工作。

The problem is that the span.hvr links are generated dynamically: when the page loads, they do not exist, but then a script generates them sometime later. 问题在于span.hvr链接是动态生成的:当页面加载时,它们不存在,但是脚本稍后会生成它们。

So for your script to work, it should wait for span.hvr elements to be present first, before doing anything further. 因此,为使脚本正常工作,它应该先等待span.hvr元素出现,然后再执行其他操作。

The following modified script should work: 以下修改的脚本应该可以工作:

// ==UserScript==
// @name       linkify span href
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      http://www.thefreedictionary.com/*
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js
// @copyright  2012+, You
// @grant      GM_log
// ==/UserScript==
//alert("hello");

jQuery(document).ready(Greasemonkey_main);

function Greasemonkey_main ()
{  
    var isReady = jQuery("span.hvr").length > 0;

    if (!isReady) {
        setTimeout(Greasemonkey_main, 500);
        return;
    }

    jQuery("span.hvr").each(function () {         
        jQuery(this).wrap('<a href="/' + jQuery(this).text() + '" style="text-decoration:none;color:inherit" target="_blank"></a>')
    });
}

If there are pages in which it is possible to have no span.hvr elements, then you should make the script a bit smarter – stop retrying after 5-10 seconds for example. 如果某些页面中可能没有span.hvr元素,则应使脚本更聪明–例如,在5-10秒后停止重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM