【问题标题】:Netfix QueueSorter (chrome) - Help with modifying script to sort groups alphabeticallyNetflix Queue Sorter (chrome) - 帮助修改脚本以按字母顺序对组进行排序
【发布时间】:2011-06-01 06:03:44
【问题描述】:

我已将Netflix Queue sorter 作为扩展安装。但是,它会随机为一个类型中的电影分配顺序。我希望它按类型中的名称排序。

我打开 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;
    };

【问题讨论】:

    标签: javascript jquery google-chrome-extension netflix


    【解决方案1】:

    我想通了。构建代码以将文章数组设置为空。这是一个小的 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 中的文件,然后安装扩展程序。

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 2022-12-01
      • 2016-09-26
      • 2010-12-12
      相关资源
      最近更新 更多