【问题标题】:Chrome userscript is not applying remote CSS stylesheetChrome 用户脚本未应用远程 CSS 样式表
【发布时间】:2013-05-02 08:31:16
【问题描述】:

直接链接到脚本:https://github.com/MosheBerman/MinimalOverflow/blob/master/styleswitcher.user.js

我从https://stackapps.com/questions/2143/minimaloverflow-a-themescript-for-stack-exchange 下载了一个脚本,并将 *.user.js 文件拖放到 Chrome 的扩展选项卡中。脚本运行,我使用alert 进行了验证。我什至在脚本中alerted href 属性以验证它是否正确更改它。

但是,每当脚本更改 CSS 时,都不会应用样式:

基本上,代码会查找link 标记并更改href 属性。

第 64 行

function switchToStylesheet(sheet){

    for(var i=0; i<linkElements.length; i++){   
        if(linkElements[i].getAttribute("rel") == "stylesheet"){

            //apply the new stylesheet
            linkElements[i].href = sheet;

            alert(linksElements[i].href); 
// shows https://github.com/MosheBerman/MinimalOverflow/raw/master/clean.css as expected

            //We only want to replace the first stylesheet, so return.
            //If SO decides to change any styles, just put it in a 
            //second stylesheet and link tag. The script should safely ignore it.
            return;
        }
    }
}

我有Version 25.0.1364.152 我正在运行Windows 7 64-bit

谁能验证这是ChromeJavascript 还是Userscript 问题?

【问题讨论】:

标签: javascript css google-chrome github userscripts


【解决方案1】:

"Referencing a .css file in github repo as stylesheet in a .html file"。 Git 使用错误的 mime 类型提供 CSS 文件 (.../raw/master/clean.css);因此 Chrome 和 Firefox 等浏览器(通常)不会处理/应用此类 CSS。

快速解决方法是将 CSS 文件托管在您自己的服务器或免费文件托管网站之一上。然后将脚本设置更改为:

var pathToNewStylesheet = "http://YOUR_SERVER.COM/YOUR_PATH/clean.css";

【讨论】:

  • 谢谢..真不敢相信我必须跳过多少圈..再也没有“可访问性”这样的东西了。
猜你喜欢
  • 2016-02-28
  • 2015-06-08
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2021-09-13
  • 2020-09-22
  • 2012-11-12
  • 2018-07-18
相关资源
最近更新 更多