0

问题:

Firefox 可以在标题中的名称之前显示图像的分辨率吗?如果图像的名称太大,我无法读取其分辨率,这让我很烦恼。

由 supasd 询问

我做了一些研究,还没有成功。

你们中有人会碰巧知道怎么做吗?最聪明/最有效的方法是什么?可以用用户风格来完成吗?还是需要用户脚本或附加组件?随意提供任何类型的建议或解决方案,无论是用户风格还是其他。

4

1 回答 1

0

最简单的代码是在加载文档时更改标题:

// ==UserScript==
// @name         Image dimensions before title
// ==/UserScript==

(function() {
    var m = document.title
                    .match(/^(.+?\.\w+) \((?:([^,]+?), )?(\d+)\s*[x×]\s*(\d+)( .+?)?\)/);
    if (m)
        document.title = m[3] + 'x' + m[4] + ' ' + m[1] + (m[2] ? ' (' + m[2] + ')' : '');
})();

使用 MutationObserver 可以在浏览器设置标题时立即更改标题而不会闪烁。正则表达式适用于 Firefox 和 Chrome,并将尺寸移动到文件名之前。事件侦听器DOMContentLoaded仍用于覆盖其他插件/用户脚本(如 CenterImage)的有害影响,它会破坏<HEAD>元素。

// ==UserScript==
// @name         Image dimensions before title
// @run-at       document-start
// ==/UserScript==

if (!changeTitle()) {
    new MutationObserver(function(mutations) {
        var nodes = mutations[0].addedNodes;
        if (nodes[0] && nodes[0].localName == 'title' && changeTitle(nodes[0])
        || document.body && document.body.children.length) {
            this.disconnect();
        }
    }).observe(document, {subtree: true, childList: true});
}
document.addEventListener('DOMContentLoaded', changeTitle);

function changeTitle(node) {
    var m = document.title
                    .match(/^(.+?\.\w+) \((?:([^,]+?), )?(\d+)\s*[x×]\s*(\d+)( \w+)?\)/);
    if (m) {
        document.title = m[3] + 'x' + m[4] + ' ' + m[1] + (m[2] ? ' (' + m[2] + ')' : '');
        return true;
    }
}
于 2015-10-05T13:02:42.740 回答