繁体   English   中英

用新链接替换网站上的许多旧链接? (客户端)

[英]Replace lots of old links on a site with new links? (client side)

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

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

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

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

目前,我有这个小片段,用!标记所有旧链接。

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

一种解决方案是对所有链接执行此操作

不要运行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;
    }
}

暂无
暂无

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

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