0

我安装了Netflix 队列排序器作为扩展。但是,它会随机为一个类型中的电影分配顺序。我希望它在类型中按名称排序。

我打开 C:\Users\MyUserName\AppData\Local\Google\Chrome\User Data\Default\Extensions\ExtensionId\1.13_0\script.js 我可以看到 javascript 仍然需要一些工作。这是我修改后的方法,它按标题和流派排序。

function sortByTitleAndGenre() {
    var articles;
    sortInfo = [];
    var pos = 1;

    var articlesKey = 'sortByTitle.articles';
    var ignoreArticlesKey = 'sortByTitle.ignoreArticles';
    var ignoreArticles = GM_getValue(ignoreArticlesKey);
    if (undefined === ignoreArticles) {
        // Use true as default as Netflix ignores articles too.
        ignoreArticles = true;

        // Store keys so that users can change it via about:config.
        GM_setValue(ignoreArticlesKey, ignoreArticles);
        // The articles are used "as-is", so there must be a space after
        // each one in most cases.  To avoid typos in the default, use [].
        articles = [
            "A ",
            "AN ",
            "THE ",
            "EL ",
            "LA ",
            "LE ",
            "LES ",
            "IL ",
            "L'"
        ];
        GM_setValue(articlesKey, articles.join(',').toUpperCase());
    }

    var elts = customGetElementsByClassName(document, 'input', 'o');
    for (var idx = 0; idx < elts.length; idx++) {
        var boxName = elts[idx].name;
        var boxId = boxName.substring(2);
        // If a movie is both at home and in the queue, or a movie has been
        // watched but is still in the queue, there is both _0 and _1.
        // Here we either one works.
        var titleId = 'b0' + boxId + '_0';
        var titleElt = document.getElementById(titleId);

        var genre = $("tr[data-mid='" + boxId + "'] .gn .genre").text().toUpperCase();

        var title = titleElt.innerHTML.toUpperCase();
        if (ignoreArticles) {
            // Get the articles, but default to empty string.
            var articlesStr = GM_getValue(articlesKey, '') || '';
            articlesStr = articlesStr.toUpperCase();
            articles = articlesStr.split(',');
            for (var aa = 0; aa < articles.length; aa++) {
                var article = articles[aa].toUpperCase();
                if (0 === title.indexOf(article)) {
                    // Move article to the end of the string.
                    title = title.substring(article.length) +
                            ', ' + article;
                    break;
                }
            }
        }

        var record = {
            "id": boxId,
            "title": title,
            "genre": genre,
            "origPos": pos++
        };
        sortInfo.push(record);
    }

    var sortFn = function (a, b) {
        if (a.genre == b.genre)
            return a.title > b.title ? -1 : 1;
        else
            return a.genre > b.genre ? -1 : 1;
    };
    sortInfo.sort(sortFn);

    setOrder("origPos", elts);
}

我的问题是,虽然它排序很好,但它并没有忽略这些文章。我的排序功能关闭了吗?我认为可以更简洁地定义它(一行)。

    var sortFn = function (a, b) {
        if (a.genre == b.genre)
            return a.title > b.title ? -1 : 1;
        else
            return a.genre > b.genre ? -1 : 1;
    };
4

1 回答 1

0

我想到了。构建代码以将文章数组设置为空。这是一个小的 WTF,但我用一些注释行修复了它。

//var articlesStr = GM_getValue(articlesKey, '') || '';
//articlesStr = articlesStr.toUpperCase();
//articles = articlesStr.split(',');

对于使用此扩展的任何人,我通过将 jquery 库添加到扩展来修复它。我在 manifest.json 中添加了对它的引用。

然后只需用此方法替换现有的 sortByGenre

function sortByGenre() {
    var articles;
    sortInfo = [];
    var pos = 1;

    var articlesKey = 'sortByTitle.articles';
    var ignoreArticlesKey = 'sortByTitle.ignoreArticles';
    var ignoreArticles = GM_getValue(ignoreArticlesKey);
    if (undefined === ignoreArticles) {
        // Use true as default as Netflix ignores articles too.
        ignoreArticles = true;

        // Store keys so that users can change it via about:config.
        GM_setValue(ignoreArticlesKey, ignoreArticles);
        // The articles are used "as-is", so there must be a space after
        // each one in most cases.  To avoid typos in the default, use [].
        articles = [
            "A ",
            "AN ",
            "THE ",
            "EL ",
            "LA ",
            "LE ",
            "LES ",
            "IL ",
            "L'"
        ];
        //GM_setValue(articlesKey, articles.join(',').toUpperCase());
    }

    var elts = customGetElementsByClassName(document, 'input', 'o');
    for (var idx = 0; idx < elts.length; idx++) {
        var boxName = elts[idx].name;
        var boxId = boxName.substring(2);
        // If a movie is both at home and in the queue, or a movie has been
        // watched but is still in the queue, there is both _0 and _1.
        // Here we either one works.
        var titleId = 'b0' + boxId + '_0';
        var titleElt = document.getElementById(titleId);

        var genre = $("tr[data-mid='" + boxId + "'] .gn .genre").text().toUpperCase();

        var title = titleElt.innerHTML.toUpperCase();
        if (ignoreArticles) {
            // Get the articles, but default to empty string.
            //var articlesStr = GM_getValue(articlesKey, '') || '';
            //articlesStr = articlesStr.toUpperCase();
            //articles = articlesStr.split(',');
            for (var aa = 0; aa < articles.length; aa++) {
                var article = articles[aa].toUpperCase();
                if (0 === title.indexOf(article)) {
                    // Move article to the end of the string.
                    title = title.substring(article.length) +
                            ', ' + article;
                    break;
                }
            }
        }

        var record = {
            "id": boxId,
            "title": title,
            "genre": genre,
            "origPos": pos++
        };
        sortInfo.push(record);
    }

    var sortFn = function (a, b) {
        if (a.genre == b.genre)
            return a.title > b.title ? -1 : 1;
        else
            return a.genre > b.genre ? -1 : 1;
    };
    sortInfo.sort(sortFn);

    setOrder("origPos", elts);
}

完成此操作后,您可以在 chrome 中进入开发人员模式,然后将其解包加载或打包为 crx,导航到 chrome 中的文件,然后安装扩展程序。

于 2010-12-29T22:15:09.993 回答