Blogger - 使用 JavaScript 获取查看计数器的值?
Blogger - get view counter's value with JavaScript?
我只能在 HTML 使用此代码完成加载后获取值:
var array = $('#Stats1_totalCount strong').map(function(){
return $(this).text()
}).get();
var vcount = '';
for (i = 0; i < array.length; i++) {
vcount += array[i];
}
console.log(vcount);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="counter-wrapper graph-counter-wrapper" id="Stats1_totalCount">
<span class="digit stage-0"><strong>1</strong></span>
<span class="digit stage-0"><strong>2</strong></span>
<span class="digit stage-0"><strong>3</strong></span>
<span class="digit stage-0"><strong>4</strong></span>
<span class="digit stage-0"><strong>5</strong></span>
</span>
但是 Blogger 使用 ajax 到 inject counter
:
Counter and image will be injected later via AJAX call.
我尝试使用 .ajaxStop
和 ajaxComplete
,其中 none 有效。
如何使用 JS 获取视图计数器的值?
感谢@RoryMcCrossan 使用 MutationObserver
的建议,我现在可以使用 JavaScript!
获取视图计数器的值
(function startObservation() {
var
/* 1) Create a MutationObserver object*/
observer = new MutationObserver(
function(mutations) {
mutationObserverCallback(mutations);
}),
/* 2) Create a config object */
config = {childList: true};
/* 3) Glue'em all */
observer.observe(Stats1_totalCount, config);
})();
function mutationObserverCallback(mutations) {
var mutationRecord = mutations[0];
if (mutationRecord.addedNodes[0] !== undefined) {
var array = $('#Stats1_totalCount strong').map(function(){
return $(this).text()
}).get();
var vcount = '';
for (i = 0; i < array.length; i++) {
vcount += array[i];
}
console.log(vcount);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="counter-wrapper graph-counter-wrapper" id="Stats1_totalCount">
<span class="digit stage-0"><strong>1</strong></span>
<span class="digit stage-0"><strong>2</strong></span>
<span class="digit stage-0"><strong>3</strong></span>
<span class="digit stage-0"><strong>4</strong></span>
<span class="digit stage-0"><strong>5</strong></span>
</span>
我只能在 HTML 使用此代码完成加载后获取值:
var array = $('#Stats1_totalCount strong').map(function(){
return $(this).text()
}).get();
var vcount = '';
for (i = 0; i < array.length; i++) {
vcount += array[i];
}
console.log(vcount);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="counter-wrapper graph-counter-wrapper" id="Stats1_totalCount">
<span class="digit stage-0"><strong>1</strong></span>
<span class="digit stage-0"><strong>2</strong></span>
<span class="digit stage-0"><strong>3</strong></span>
<span class="digit stage-0"><strong>4</strong></span>
<span class="digit stage-0"><strong>5</strong></span>
</span>
但是 Blogger 使用 ajax 到 inject counter
:
Counter and image will be injected later via AJAX call.
我尝试使用 .ajaxStop
和 ajaxComplete
,其中 none 有效。
如何使用 JS 获取视图计数器的值?
感谢@RoryMcCrossan 使用 MutationObserver
的建议,我现在可以使用 JavaScript!
(function startObservation() {
var
/* 1) Create a MutationObserver object*/
observer = new MutationObserver(
function(mutations) {
mutationObserverCallback(mutations);
}),
/* 2) Create a config object */
config = {childList: true};
/* 3) Glue'em all */
observer.observe(Stats1_totalCount, config);
})();
function mutationObserverCallback(mutations) {
var mutationRecord = mutations[0];
if (mutationRecord.addedNodes[0] !== undefined) {
var array = $('#Stats1_totalCount strong').map(function(){
return $(this).text()
}).get();
var vcount = '';
for (i = 0; i < array.length; i++) {
vcount += array[i];
}
console.log(vcount);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="counter-wrapper graph-counter-wrapper" id="Stats1_totalCount">
<span class="digit stage-0"><strong>1</strong></span>
<span class="digit stage-0"><strong>2</strong></span>
<span class="digit stage-0"><strong>3</strong></span>
<span class="digit stage-0"><strong>4</strong></span>
<span class="digit stage-0"><strong>5</strong></span>
</span>