gpt4 book ai didi

javascript - 检查浏览器是否有 Pinterest Pin it 按钮

转载 作者:数据小太阳 更新时间:2023-10-29 05:49:38 24 4
gpt4 key购买 nike

我编写了一个脚本来为我网站上的大多数图片添加一个 Pinterest 按钮。问题是当有人拥有 Chrome Pin it extension在他们的浏览器中启用时,“固定它”按钮会为用户显示两次。

无论如何,在 JavaScript 中,是否有检查用户是否在他们的浏览器中启用了这个扩展?

(function($) {

$(function() {

$('.container img').each(function() {
if ($(this).parent('a')) {
var $permalink = $(this).parent('a').attr('href');
}
else {
var $permalink = $(location).attr('href');
}


var $permalink = $(location).attr('href'),
$title = $('h1.product_name').text() || $('h2.header');

var $linkhtml = $('<a/>', {
'class':'pin-it-button pinme',
'html': '<img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" />',
'count-layout': 'horizontal',
'style': 'cursor:pointer; position:absolute; bottom:30px; left:0; border:0 none; opacity: 0.4;',
'href': 'http://pinterest.com/pin/create/button/?url=' + $permalink + '&media=' + $(this).attr('src') + '&description=' + $title
});

if ($(this).parent('a')) {
$(this).addClass('pinme').parent('a').after($linkhtml);
}
else {
$(this).addClass('pinme').after($linkhtml);
}

$('.pinme').hover(
function() {
if ($(this).hasClass('pin-it-button')) {
console.log('hello');
$(this).css('opacity', '1');
}
else {
$(this).parent().siblings('.pin-it-button').css('opacity', '1');
}
}, function() {
if ($(this).hasClass('pin-it-button')) {
$(this).css('opacity', '0.4');
}
else {
$(this).parent().siblings('.pin-it-button').css('opacity', '0.4');
}
}
);


});
});
})(jQuery);

最佳答案

新的 Pinterest 扩展 (2017)

注入(inject) <span> <body 正下方的悬停按钮:

<span style="border-radius: 3px;
text-indent: 20px;
width: auto;
padding: 0px 4px 0px 0px;
text-align: center;
font-style: normal;
font-variant: normal;
font-weight: bold;
font-stretch: normal;
font-size: 11px;
line-height: 20px;
font-family: &quot;Helvetica Neue&quot;, Helvetica, sans-serif;
color: rgb(255, 255, 255);
background: url(&quot;data:image/svg+xml;base64,P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w4Ljc2NyAxNC42NzgsOC43NjcgQzEwLjk1OSw4Ljc2NyA4Ljc3NywxMS41MzYgOC43NzcsMTQuMzk4IEM4Ljc3NywxNS41MTMgOS4yMSwxNi43MDkgOS43NDksMTcuMzU5IEM5Ljg1NiwxNy40ODggOS44NzIsMTcuNiA5Ljg0LDE3LjczMSBDOS43NDEsMTguMTQxIDkuNTIsMTkuMDIzIDkuNDc3LDE5LjIwMyBDOS40MiwxOS40NCA5LjI4OCwxOS40OTEgOS4wNCwxOS4zNzYgQzcuNDA4LDE4LjYyMiA2LjM4NywxNi4yNTIgNi4zODcsMTQuMzQ5IEM2LjM4NywxMC4yNTYgOS4zODMsNi40OTcgMTUuMDIyLDYuNDk3IEMxOS41NTUsNi40OTcgMjMuMDc4LDkuNzA1IDIzLjA3OCwxMy45OTEgQzIzLjA3OCwxOC40NjMgMjAuMjM5LDIyLjA2MiAxNi4yOTcsMjIuMDYyIEMxNC45NzMsMjIuMDYyIDEzLjcyOCwyMS4zNzkgMTMuMzAyLDIwLjU3MiBDMTMuMzAyLDIwLjU3MiAxMi42NDcsMjMuMDUgMTIuNDg4LDIzLjY1NyBDMTIuMTkzLDI0Ljc4NCAxMS4zOTYsMjYuMTk2IDEwLjg2MywyNy4wNTggQzEyLjA4NiwyNy40MzQgMTMuMzg2LDI3LjYzNyAxNC43MzMsMjcuNjM3IEMyMS45NSwyNy42MzcgMjcuODAxLDIxLjgyOCAyNy44MDEsMTQuNjYyIEMyNy44MDEsNy40OTUgMjEuOTUsMS42ODYgMTQuNzMzLDEuNjg2IiBmaWxsPSIjYmQwODFjIj48L3BhdGg+PC9nPjwvc3ZnPg==&quot;) 3px 50% / 14px 14px no-repeat rgb(189, 8, 28);
position: absolute;
opacity: 1;
z-index: 8675309;
display: none;
cursor: pointer;
border: none;
-webkit-font-smoothing: antialiased;
top: 240px;
left: 110px;
">Save</span>

所以一个简单的检查是:

var pin = document.querySelector('body > span[style*="8675309"][style*="rgb(189, 8, 28)"]')

或者您可以检查包含 P 标志的整个背景 base64 字符串。


旧 PinIt 扩展的旧答案:

检查安装了 Pin It 扩展的页面,我们可以看到它向 <body> 添加了自己的属性:

<body data-pinterest-extension-installed="cr1.39.1">

在js中很容易判断属性是否存在:

if (document.body.dataset.pinterestExtensionInstalled) {
console.log("Pin It extension detected!");
}

请注意,该属性是在页面加载后添加的,因此您无法在DOMContentLoaded 中正确检查它。事件处理程序;用 setInterval 暂停一下或使用 MutationObserver :

  • 带有 "run_at": "document_end" 的内容脚本或 "document_idle" (默认模式):

    var PinItInstalled = undefined;

    new MutationObserver(function(mutations) {
    PinItInstalled = document.body.dataset.pinterestExtensionInstalled;
    this.disconnect();
    }).observe(document.body, {
    attributes: true,
    attributeFilter: ["data-pinterest-extension-installed"]
    });
  • 带有 "run_at": "document_start" 的内容脚本:

    var PinItInstalled = undefined;

    document.addEventListener("DOMContentLoaded", function() {
    new MutationObserver(function(mutations) {
    PinItInstalled = document.body.dataset.pinterestExtensionInstalled;
    this.disconnect();
    }).observe(document.body, {
    attributes: true,
    attributeFilter: ["data-pinterest-extension-installed"]
    });
    });

附言不要忘记测试如果禁用 Pin It 扩展的悬停时显示其按钮的选项会发生什么情况。

关于javascript - 检查浏览器是否有 Pinterest Pin it 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33305676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com