【问题标题】:Replace lots of old links on a site with new links? (client side)用新链接替换网站上的大量旧链接? (客户端)
【发布时间】:2013-03-08 08:24:40
【问题描述】:

我们目前正在将文章从旧 CMS 迁移到新 CMS。问题是旧文章中的链接是硬编码的,并且引用的是旧网站。

我们迁移者无法访问数据库,因此所有文章和所有链接都必须手动更改。
我有旧的站点地图和新的站点地图,并且想编写一个 Greasemonkey/jQuery 脚本来单击一个按钮,该脚本会将所有 old-cms-links 更改为 new-cms-links。

在这种情况下,最好/最优雅的解决方案是什么?使用数组?有 500 多个网站/文章...

您建议记住什么,只允许客户端脚本?

目前我有这个小sn-p,它用!标记所有旧链接。

$("#links").click(function() 
{
    $('a[href*="pattern-of-old-cms"]')
        .append('<span class="attention" style="font-size: 25px; color:red;">!</span>');
});

【问题讨论】:

  • 你不能在你的情况下使用.htaccess 吗?这将是最好的解决方案,使用[R=301] 标签。
  • 不,只有客户端访问 :(

标签: javascript jquery content-management-system greasemonkey


【解决方案1】:
$("a[href='http://www.google.com/']").attr('href', 'http://www.live.com/'); 

一种解决方案是对所有链接都这样做

【讨论】:

  • 当然,但是有 500 个不同的链接?
  • 几乎没有,旧的链接是这样的 /index.php?id=181 而新的链接有友好的 url,比如 /about-us 看起来你的 jquery 解决方案是唯一的......跨度>
  • 加载一个javascript,它在地图中将旧网址映射到新网址。然后使用此处建议的方法相应地替换路径
  • 制作和 Excel 电子表格,其中: A 列:$("a[href=' B 列:旧链接 C 列:']").attr('href',' D 列:新链接E栏:');复制记事本中的所有内容并将选项卡替换为空。完成
  • 谢谢!我想我会在 Brock 的数组解决方案中使用你的 excel 技巧
【解决方案2】:

不要运行 500+ .attr() 语句!使用更有效的方式处理页面。

像这样创建两个文件:

old_URLs.js:

var oldUrlArray = [
    "Old address 1",
    "Old address 2",
    "Old address 3",
    // etc., etc.
]

new_URLs.js:

var newUrlArray = [
    "New address 1",
    "New address 2",
    "New address 3",
    // etc., etc.
]

并将它们放在与您的 gm.user.js 文件相同的文件夹中。

那么你的脚本就变成了:

// ==UserScript==
// @name     _Mass link replacer remapper
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  old_URLs.js
// @require  new_URLs.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
var oldLinks = $('a[href*="pattern-of-old-cms"]');

while (oldLinks.length) {
    var firstHref = oldLinks[0].href;
    var hrefIndex = oldUrlArray.indexOf (firstHref);

    if (hrefIndex >= 0) {
        var toReplace   = oldLinks.filter ("[href='" + firstHref + "']");
        toReplace.attr ("href", newUrlArray[hrefIndex]);

        oldLinks = oldLinks.not (toReplace);
    }
    else {
        alert ("I don't know how to map the link: " + firstHref);
        break;
    }
}

【讨论】:

  • 非常感谢!我认为这会很好,我已经考虑过用数组解决这个任务但需要提示:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
相关资源
最近更新 更多