Last active
March 19, 2018 04:13
-
-
Save kjirou/7809b89eff02d74bb0d820772d3505d4 to your computer and use it in GitHub Desktop.
Rollbar管理画面のclient.javascript.browserにブラウザ名を表示するブックマークレット
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
if ( | |
document.URL.indexOf('https://rollbar.com/') === -1 || | |
document.URL.indexOf('/occurrences/') === -1 | |
) { | |
alert('"https://rollbar.com/:変数/:変数/items/:変数/occurrences/:変数/" のURLで実行することを想定したスニペットです。'); | |
return; | |
} | |
if (!window.UAParser) { | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
// From) https://github.com/faisalman/ua-parser-js | |
script.src = 'https://cdn.jsdelivr.net/npm/ua-parser-js@0/dist/ua-parser.min.js'; | |
document.body.appendChild(script); | |
} | |
function findUaString() { | |
var uaStr = null; | |
Array.prototype.forEach.call(document.querySelectorAll('.params-table tr'), function(tr) { | |
var itemNameTd = tr.children[0]; | |
if (itemNameTd && itemNameTd.textContent === 'client.javascript.browser') { | |
uaStr = tr.children[1].textContent; | |
return; | |
} | |
}); | |
if (uaStr === null) { | |
throw new Error('"client.javascript.browser"の項目が見つかりませんでした。'); | |
} | |
return uaStr; | |
} | |
function renderBrowserInformation(result) { | |
var browserInfo = 'Browser=(' + result.browser.name + ',' + result.browser.version + ')'; | |
browserInfo += ' OS=(' + result.os.name + ',' + result.os.version + ')'; | |
Array.prototype.forEach.call(document.querySelectorAll('.params-table tr'), function(tr) { | |
var itemNameTd = tr.children[0]; | |
if (itemNameTd && itemNameTd.textContent === 'client.javascript.browser') { | |
var uaTd = tr.children[1]; | |
var span = document.createElement('span'); | |
span.textContent = browserInfo; | |
span.style.color = 'red'; | |
uaTd.appendChild(span); | |
return; | |
} | |
}); | |
} | |
function main() { | |
var parser = new UAParser(); | |
var uaStr = findUaString(); | |
parser.setUA(uaStr); | |
var result = parser.getResult(); | |
renderBrowserInformation(result); | |
console.log('parser.getResult():', result); | |
console.log('Snippet completed'); | |
} | |
// Run main function only once | |
var counter = 0; | |
setTimeout(function() { | |
if (UAParser) { | |
main(); | |
} else if (counter < 10) { | |
counter++; | |
setTimeout(main, 200); | |
} | |
}, 50); | |
}).call(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bookmarklet