简体   繁体   English

Tampermonkey脚本将文本转换为链接

[英]Tampermonkey script to turn text into links

Another poor soul here who can't make heads or tails of text replacement in Tampermonkey/Greasemonkey. 另一个可怜的灵魂在Tampermonkey / Greasemonkey中无法进行文本替换的正面或反面。 I'm dealing with a series of table rows like so: 我正在处理一系列表行,如下所示:

<table class="table table-striped table-hover">
    <tr>
        [a bunch of header cells]
    </tr>
    <tr>
        <td>412</td>
        <td>An image</td>
        <td>A thing</td>
        <td>A person</td>
        <td>A place</td>
    </tr>
    <tr>
        <td>3789</td>
        <td>An image</td>
        <td>A thing</td>
        <td>A person</td>
        <td>A place</td>

What I want is to turn each of the numbers into a link of this form: 我想要的是将每个数字转换为这种形式的链接:

        <td><a href="https://mydomain.com/workflow/index.html?
             endrun=1&submit=Edit&record=3789">3789</a></td>

(Line break after the ? is just so it will fit on the page here; there should not be an actual line break in the URL.) (在?后面的换行符恰好在这里适合页面; URL中不应该有实际的换行符。)

There are multiple rows in the table; 表中有多行; each one starts with a number that I would like to linkify. 每一个都以我想要链接的数字开头。 On some pages on the site, the number is the third or fourth item in each row rather than the first. 在网站的某些页面上,该数字是每行中的第三个或第四个项目,而不是第一个。 Each page has only one table. 每个页面只有一个表。 I don't want to change the first cell in the header row, since those cells are already clickable and used for sorting, and I don't want to linkify any of the words--just the numbers. 我不想更改标题行中的第一个单元格,因为这些单元格已经可以点击并用于排序,我不想链接任何单词 - 只是数字。

I looked at Most concise way to do text replacement on a web page? 我看了最简洁的方法来在网页上进行文本替换? (using GreaseMonkey) and How to create links from existing text in Greasemonkey? (使用GreaseMonkey)如何从Greasemonkey中的现有文本创建链接? and Greasemonkey: Add a link in a table and http://www.overclock.net/t/487779/help-with-greasemonkey-and-regular-expressions but I can't quite figure out how to make any of the suggested answers work for my situation. Greasemonkey:在表格中添加一个链接http://www.overclock.net/t/487779/help-with-greasemonkey-and-regular-expressions但我无法弄清楚如何制作任何建议答案适用于我的情况。 (I'm running Tampermonkey in Chrome, but hoped Greasemonkey answers might work for me.) I would humbly appreciate any help. (我在Chrome中运行Tampermonkey,但希望Greasemonkey的答案对我有用。)我会谦卑地感谢任何帮助。

This should do it for tampermonkey. 这应该用于tampermonkey。

Just need to update the namespace to the one you want it to run against. 只需要将命名空间更新为您希望它运行的命名空间。 If the site already uses jQuery then you can ditch the require attribute as well. 如果该站点已经使用了jQuery,那么您也可以抛弃require属性。

 // ==UserScript== // @name Updator // @namespace http://stackoverflow.com/ // @version 0.1 // @description enter something useful // @match http://*/* // @copyright 2012+, You // @require http://code.jquery.com/jquery-2.1.1.min.js // ==/UserScript== $(document).on("ready", function() { $("table.table-striped td:first-child").each(function(e, i){ var this$ = $(this); var q = this$.html() if (/^\\d+$/ig.test(q)){ this$.html("<a href='https://mydomain.com/workflow/index.html?endrun=1&submit=Edit&record=" + q + "' >" + q + "</a>"); } }); }); 

if you don't use jQuery you can also do something like this: 如果你不使用jQuery,你也可以这样做:

    var cells = [];
    var coll1 = document.getElementsByTagName('TH');
    var coll2 = document.getElementsByTagName('TD');
    for (var i = 1; i < coll1.length; i++) {
        cells.push(coll1[i]);
    }
    for (var i = 0; i < coll2.length; i++) {
        cells.push(coll2[i]);
    }
    for (var i = 0; i < cells.length; i++) {
      var cellValue = cells[i].innerHTML;
      var number = cellValue.match(/\d+/)[0];//get first number in a cell
      cells[i].innerHTML = cellValue.replace(/\d+/, '<a href="https://mydomain.com/workflow/index.html?endrun=1&submit=Edit&record=' + number + '">' + number + '</a>');
   }

edit used your link 编辑使用了您的链接

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

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