简体   繁体   中英

Get value data and change according to each link?

How to get the value of "data-token" down to "??????????????????" in the script. And it changes with each link? For example:

  • "Download 01" link uses token: '1111',

  • "Download 02" link uses token: '2222',

  • "Download 04" link uses token: '444444',

<a data-token="1111" href="https://example.com/download=12345">Download 01</a>
<a data-token="2222" href="https://example.com/download=123456">Download 02</a>
<a data-token="444444" href="https://example.com/download=123457">Download 04</a>
<script>
window.linkurl = {
token: '??????????????????',
domains: ``,
patterns: `download=`
};
(function() {
    function doHash(el, token) {
        el.href = 'https://example.com/full/?api='
            + token
            + '&url='+btoa(el.href)+'=&type=1';
        console.log(el.href);
    }

    var token = linkurl.token || '';
    var domains = linkurl.domains.split('\n') || [];
    domains.push('example.com');
    domains.push('www.example.com');
    domains.push(location.hostname);
    var patterns = linkurl.patterns.split('\n').filter(function(item) {return Boolean(item)}) || [];
    var aTags = document.querySelectorAll('a[href]:not([href^="javascript"]):not([onclick]):not([ng-click])');
    aTags.forEach(function(el) {
        if (domains.indexOf(el.hostname) === -1) {
            doHash(el, token);
            return;
        }
        patterns.forEach(function(pattern) {
            try {
                var regex = new RegExp(pattern);
                if(regex.test(el.href)) doHash(el, token);
            } catch (e) {console.log(e)}
        });
    });
})();
</script>

Update link: http://jsfiddle.net/Lu8shmco/

You can use Element.dataset property, it provides read/write access to from data-* prefixed custom attributes.

Thus you can get value using

el.dataset.token

 window.linkurl = { token: '??????????????????', domains: ``, patterns: `download=` }; function doHash(el) { el.href = 'https://example.com/full/?api=' + el.dataset.token + '&url=' + btoa(el.href) + '=&type=1'; console.log(el.href); } var domains = linkurl.domains.split('\\n') || []; domains.push('example.com'); domains.push('www.example.com'); domains.push(location.hostname); var patterns = linkurl.patterns.split('\\n').filter(function(item) { return Boolean(item) }) || []; var aTags = document.querySelectorAll('a[href]:not([href^="javascript"]):not([onclick]):not([ng-click])'); aTags.forEach(function(el) { if (domains.indexOf(el.hostname) === -1) { doHash(el); return; } patterns.forEach(function(pattern) { try { var regex = new RegExp(pattern); if (regex.test(el.href)) doHash(el); } catch (e) { console.log(e) } }); }); 
 <a data-token="1111" href="https://example.com/download=12345">Download 01</a> <a data-token="2222" href="https://example.com/download=123456">Download 02</a> <a data-token="444444" href="https://example.com/download=123457">Download 04</a> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM