如何使用Javascript删除非自托管网站上的收藏夹


<link rel="shortcut icon" href="https://files.writeas.org/favicon.ico" />

代码在网站中吗,我不能更改它,我只能更改CSS或注入我自己的Javascript,因此我如何使用Javascript获取HTML代码的这一部分并注释掉/覆盖它,从而删除它?

如果不能删除它,可以使用以下代码进行更改(注意:删除可能不够,因为许多Bowser默认为/faveicon.ico(

//const ico_url = document.location.origin + '/favicon.ico'
const ico_url = 'https://api.adorable.io/avatar/256/your_icon.png'
let head = document.getElementsByTagName('head')[0]
console.dir(head)
var link;
let links = head.getElementsByTagName('link')
for(let i=0; i<links.length; i++) {
if (links[i].rel == 'icon' || links[i].rel == 'shortcut icon') {
link = links[i]
console.log('old-icon: '+link.href)
link.href = ico_url
console.log('new-icon: '+link.href)

}
}
if (typeof(link) == 'undefined') {
console.log('creating favicon link !')
link = document.createElement('link')
link.setAttribute('rel','icon')
link.setAttribute('type','image/png')
link.setAttribute('href',ico_url) // new favicon ...
document.getElementsByTagName('head')[0].appendChild(link)
}
console.log(link.href)

也许您可以尝试替换head标签。。。

let ico_url = 'https://api.adorable.io/avatar/256/head_icon.png'
var head = document.createElement("head");
link = document.createElement('link')
link.setAttribute('rel','icon')
link.setAttribute('type','image/png')
link.setAttribute('href',ico_url) // new favicon ...
head.appendChild(link)
const html = document.getElementsByTagName('html')[0]
html.appendChild(head)
html.replaceChild(head,document.getElementsByTagName('head')[0])
console.dir(head)

最新更新