1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-18 07:44:51 +00:00
TiddlyWiki5/plugins/tiddlywiki/browser-sniff/browser.js
lin onetwo 95e0fac655
Expose isMobile to browser info (#6675)
* feat: expose isMobile to info

* feat: allow access browser info from $tw.browser

* fix: adapt typo

* refactor: only export selected properties

 Jermolene 5 hours ago

The trouble is that the properties of bowser.browser are not under our direct control, and so subsequent updates to Bowser might overwrite important properties of our own. I'd rather explicitly import the properties that we choose to support.

* refactor: put things into `is`
2022-05-14 14:02:52 +01:00

83 lines
2.6 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/browser-sniff/sniff.js
type: application/javascript
module-type: info
Initialise $:/info/browser tiddlers
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.getInfoTiddlerFields = function() {
var mapBoolean = function(value) {return value ? "yes" : "no";},
infoTiddlerFields = [];
// Basics
if($tw.browser) {
// Mappings from tiddler titles (prefixed with "$:/info/browser/") to bowser.browser property name
var bowser = require("$:/plugins/tiddlywiki/browser-sniff/bowser/bowser.js"),
mappings = [
["name","name","unknown"],
["version","version"],
["is/webkit","webkit"],
["is/gecko","gecko"],
["is/chrome","chrome"],
["is/firefox","firefox"],
["is/ios","ios"],
["is/iphone","iphone"],
["is/ipad","ipad"],
["is/ipod","ios"],
["is/opera","opera"],
["is/phantomjs","phantomjs"],
["is/safari","safari"],
["is/seamonkey","seamonkey"],
["is/blackberry","blackberry"],
["is/webos","webos"],
["is/silk","silk"],
["is/bada","bada"],
["is/tizen","tizen"],
["is/sailfish","sailfish"],
["is/android","android"],
["is/windowsphone","windowsphone"],
["is/firefoxos","firefoxos"],
["is/mobile","mobile"]
];
$tw.browser = $tw.utils.extend($tw.browser, {
is: bowser.browser,
});
$tw.utils.each(mappings,function(mapping) {
var value = bowser.browser[mapping[1]];
if(value === undefined) {
value = mapping[2];
}
if(value === undefined) {
value = false;
}
if(typeof value === "boolean") {
value = mapBoolean(value);
}
infoTiddlerFields.push({title: "$:/info/browser/" + mapping[0], text: value});
});
// Set $:/info/browser/name to the platform with some changes from Bowser
var platform = bowser.browser.name;
if("iPad iPhone iPod".split(" ").indexOf(platform) !== -1) {
platform = "iOS";
}
infoTiddlerFields.push({title: "$:/info/browser/name", text: platform});
// Non-bowser settings for TiddlyFox and TiddlyDesktop
var hasTiddlyFox = !!document.getElementById("tiddlyfox-message-box"), // Fails because message box is added after page load
isTiddlyDesktop = false; // Can't detect it until we update TiddlyDesktop to have a distinct useragent string
//infoTiddlerFields.push({title: "$:/info/browser/has/tiddlyfox", text: mapBoolean(hasTiddlyFox)});
//infoTiddlerFields.push({title: "$:/info/browser/is/tiddlydesktop", text: mapBoolean(isTiddlyDesktop)});
if(isTiddlyDesktop) {
infoTiddlerFields.push({title: "$:/info/browser/name", text: "TiddlyDesktop"});
}
}
return infoTiddlerFields;
};
})();