如何用jquery,ajax返回<link href =“”/> href?

时间:2016-10-31 20:29:02

标签: javascript jquery html ajax

我正在制作Chrome扩展程序,我需要知道如何使用var或其他方式获取源代码所在网站上href标记的< link> < link href="//api-cdn.github.io/stylesheetfile.css" rel="stylesheet" /> 我希望它取href值并替换它在我的代码下面的位置在这里插入样式表href链接它接近我的代码的末尾我希望脚本采取来自网站上html文件的href值,并在运行我的扩展程序时自动将href放置在该位置。我该怎么做? < script>标记也是如此,但javascript文件的标记为< script src="//api-cdn.github.io/javascriptfile.js">

我的代码

// Start github.com script
//// Create Script for Jquery
$(document).ready(function() {
  //// Create the element
  var script = document.createElement("script");
  script.className = "jquery-1.9.1";
  //// Add script content
  //// Append
  document.head.appendChild(script);
});
// End Script for Jquery
// This is a better way of removeing the stylesheets without messing with the other <link> tags, this reduces error messages in the dev console also
var elements = document.querySelectorAll('link[rel=stylesheet]');
for (var i = 0; i < elements.length; i++) {
  elements[i].parentNode.removeChild(elements[i]);
}
// This is a better way of removing the scripts without messing with the other <script> tags, this reduces error messages in the dev console also
var elements = document.querySelectorAll('script');
for (var i = 0; i < elements.length; i++) {
  elements[i].parentNode.removeChild(elements[i]);
}
// Changes the original script linking to assets-cdn.github.com, changes the href & src to # so it wont load any files from assets-cdn.github.com
/// If it loads from that site then the extension will break and the site wont work anymore
//$(document).ready(function() {
//$("script").attr("src", "#");
//$("link").attr("href", "#");
//});
// End Change Script
// Create Element for Script 1
$(document).ready(function() {
  // Create the element
  var script = document.createElement("script");
  script.className = "github-79eedeccbf089da4cd43e8b5299e4ac33d0fa03caa42e936649c23e1b2183ab8";
  // Add script content
  // Append
  document.body.appendChild(script);
});
// End of script 1
// Create Element for Script 2
$(document).ready(function() {
  // Create the element
  var script = document.createElement("script");
  script.className = "frameworks-35b9c541d341309b930b1c790fa1b27a30c7c44ce10c7f8242890e3d83c8adbd";
  // Add script content
  // Append
  document.body.appendChild(script);
});
// End of Script 2
// Create Element for Style 1
$(document).ready(function() {
  // Create the element
  var style = document.createElement("style");
  style.className = "frameworks-7a356da712cd13c4e4cfbdc04cf886bb391f84a7e92f9f7b3abf2b1034fea6e9";
  // Add script content
  // Append
  document.head.appendChild(style);
});
// End of Style 1
// Create Element for Style 2
$(document).ready(function() {
  // Create the element
  var style = document.createElement("style");
  style.className = "github-85c605289f6f5d0832a168f8ad71a38dff98eed69103be797a45c54c0cca6cac";
  // Add script content
  // Append
  document.head.appendChild(style);
});
// End of Style 2
// Fill Script Jquery-1.9.1
$(document).ready(function() {
  $.ajax({
    url: "//code.jquery.com/jquery-1.9.1.js",
    dataType: "text",
    success: function(data) {
      $(".jquery-1.9.1").html(data);
    }
  });
});
// End Script Jquery-1.9.1
// Fill Script 1
$(document).ready(function() {
  $.ajax({
    url: "//assets-cdn.github.com/assets/github-79eedeccbf089da4cd43e8b5299e4ac33d0fa03caa42e936649c23e1b2183ab8.js",
    dataType: "text",
    success: function(data) {
      $(".github-79eedeccbf089da4cd43e8b5299e4ac33d0fa03caa42e936649c23e1b2183ab8").html(data);
    }
  });
});
// End Script 1
// Fill Script 2
$(document).ready(function() {
  $.ajax({
    url: "//assets-cdn.github.com/assets/frameworks-35b9c541d341309b930b1c790fa1b27a30c7c44ce10c7f8242890e3d83c8adbd.js",
    dataType: "text",
    success: function(data) {
      $(".frameworks-35b9c541d341309b930b1c790fa1b27a30c7c44ce10c7f8242890e3d83c8adbd").html(data);
    }
  });
});
// End Script 2
// Fill Style 1
$(document).ready(function() {
  //var href = $('link[rel=stylesheet]:nth-of-type(1)').attr('href');
  $.ajax({
    url: "//assets-cdn.github.com/assets/frameworks-7a356da712cd13c4e4cfbdc04cf886bb391f84a7e92f9f7b3abf2b1034fea6e9.css",
    dataType: "text",
    success: function(data) {
      $(".frameworks-7a356da712cd13c4e4cfbdc04cf886bb391f84a7e92f9f7b3abf2b1034fea6e9").html(data);
    }
  });
});
// End Style 1
// Fill Style 2
$(document).ready(function() {
  $.ajax({
    url: "inserts the href stylesheet link here",
    dataType: "text",
    success: function(data) {
      $(".github-85c605289f6f5d0832a168f8ad71a38dff98eed69103be797a45c54c0cca6cac").html(data);
    }
  });
});
// End Style 2
// End of github.com script

1 个答案:

答案 0 :(得分:0)

我不相信他们会为您提供有关这些标签的足够信息。看起来你已经在你注释掉的一行中接近了。

如果您需要对其所有链接和脚本标记执行此操作,则可以在变量中获取所需的所有元素,并循环遍历它们。在循环时,您只需将其href值插入代码中即可。