Skip to content

Instantly share code, notes, and snippets.

@stomita
Last active May 5, 2024 21:10
Show Gist options
  • Save stomita/1436661 to your computer and use it in GitHub Desktop.
Save stomita/1436661 to your computer and use it in GitHub Desktop.
/*global sforce:true */
(function() {
function loadScript(url, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
if (callback) {
script.onload = callback;
script.onreadystatechange = function() {
if (script.readyState === 'loaded' || script.readyState === 'complete') {
script.onreadystatechange = script.onload = null;
callback();
}
};
}
document.body.appendChild(script);
}
function parseQueryString(qstr) {
var params = {};
var pairs = qstr.split('&');
for (var i=0, len=pairs.length; i<len; i++) {
var pair = pairs[i].split('=');
params[pair[0]] = decodeURIComponent(pair[1]);
}
return params;
}
var version, jsFileUrl;
if (!/\.salesforce\.com$/.test(location.hostname)) {
alert('This bookmarklet can only be used in *.salesforce.com domain.');
return;
}
var sid = document.cookie.match(/(^|;\s*)sid=(.+?)(;|$)/)[2];
if (!sid) {
alert('No sid cookie saved.');
return;
}
var scripts = document.getElementsByTagName('script');
for (var i=0, len=scripts.length; i<len; i++) {
var scriptUrl = scripts[i].src || '';
var m = scriptUrl.match(/\/sfdc-bookmarklet-loader\.js#(.+)$/);
if (m) {
var qparams = parseQueryString(m[1]);
version = qparams.v;
jsFileUrl = qparams.u;
break;
}
}
var script = document.createElement('script');
loadScript('/soap/ajax/' + (version || '31.0')+ '/connection.js', function() {
loadScript('//cdnjs.cloudflare.com/ajax/libs/jsforce/1.2.1/jsforce.min.js', function() {
sforce.connection.sessionId = sid;
jsforce.browser.connection = new jsforce.Connection({ sessionId: sid });
if (jsFileUrl) {
loadScript(jsFileUrl);
} else {
sforce.debug.open();
}
});
});
})();
<!DOCTYPE html>
<html>
<head>
<title>Ajax Toolkit Bookmarklet Loader</title>
<style type="text/css">
div.section {
margin: 20px auto;
}
input.long {
width: 400px;
}
</style>
<script type="text/javascript">
function getEl(id) {
return document.getElementById(id);
}
function generateFromGist() {
var gistId = getEl('gistId').value;
var filename = getEl('filename').value;
var title = getEl('gistTitle').value;
if (!gistId || !filename) {
alert('No gist id or filename is specified.');
return;
}
if (!title) title = filename;
getEl('gistResult').innerHTML = '';
getEl('gistResult').appendChild(
generateBookmarklet(title, 'https://cdn.rawgit.com/stomita/' + gistId + '/raw/' + filename)
)
}
function generateFromUrl() {
var jsUrl = getEl('jsUrl').value;
var title = getEl('jsTitle').value;
if (!jsUrl) {
alert('No JavaScript file URL is specified.');
return;
}
if (!title) title = jsUrl.split('/').pop();
getEl('jsResult').innerHTML = '';
getEl('jsResult').appendChild(
generateBookmarklet(title, jsUrl)
)
}
function generateToolkit() {
var version = getEl('version').value;
getEl('toolkitResult').innerHTML = '';
getEl('toolkitResult').appendChild(
generateBookmarklet("Ajax Toolkit Shell (v" + version + ")")
);
}
function generateBookmarklet(title, url) {
var a = document.createElement('a');
var version = getEl('version').value || '23.0';
a.title = title;
a.innerHTML = title;
var qparams = 'v=' + version;
if (url) {
qparams += '&u=' + encodeURIComponent(url);
}
a.href = "javascript:(function(d,u,s){s=d.createElement('script');s.type='text/javascript';s.src=u;d.body.appendChild(s)})(document,'https://cdn.rawgit.com/stomita/1436661/raw/sfdc-bookmarklet-loader.js#" + qparams + "')";
return a;
}
</script>
</head>
<body>
<h1>Ajax Toolkit Bookmarklet Loader</h1>
<div class="section">
This is the bookmarklet to load Ajax Toolkit as bookmarklet. <br/>
By default, it pops up the Ajax Toolkit shell, which enables interactive API call inside of the browser window. <br/>
Please register it in your web browser's bookmark, and then you can use sforce API whenever you are logging in to any organization of salesforce.com.
<div id="toolkitResult">
</div>
<div>
API Version : <input type="text" name="version" id="version" value="23.0"><br/>
<input type="button" onclick="generateToolkit()" value="Re-Generate Bookmarklet" />
</div>
<script type="text/javascript">
generateToolkit();
</script>
</div>
<div class="section">
<p>
1. You can load additional JavaScript file after the load of Ajax Toolkit. In the script you can assure the usage of "sforce" object in authorized context. <br/>
Please specify the file URL and create a bookmarklet to load it.
</p>
<div>
JavaScript URL : <input type="text" class="long" id="jsUrl"><br/>
Title of Bookmarklet : <input type="text" name="jsTitle" id="jsTitle"><br/>
<input type="button" onclick="generateFromUrl()" value="Generate Bookmarklet" />
</div>
<div id="jsResult">
</div>
</div>
<div class="section">
<p>
2. You can also specify your own Gist (snippet script hosting in GitHub) snippet. <br/>
To refer your script, please input your id of Gist and the script filename. <br/>
For example, if your gist url is "https://gist.github.com/1436661", input "1436661" as "Gist ID", and if the loading JavaScript filename is "toggle-language.js", put it in "Filename" field.
</p>
<div>
Gist ID : <input type="text" name="gistId" id="gistId"><br/>
Filename : <input type="text" name="filename" id="filename"><br/>
Title of Bookmarklet : <input type="text" name="gistTitle" id="gistTitle"><br/>
<input type="button" onclick="generateFromGist()" value="Generate Bookmarklet" />
</div>
<div id="gistResult">
</div>
</div>
</body>
</html>
<html>
<head>
<title>Toggle Language</title>
</head>
<body>
<p>
Drag-and-drop the below link onto your bookmark bar and register it as a bookmarklet.
</p>
<p>
Click the bookmarklet when you are logging in Salesforce, which will change the login user's display language.
</p>
<a href="javascript:(function(d,u,s)%7Bs=d.createElement('script');s.type='text/javascript';s.src=u;d.body.appendChild(s)%7D)(document,'https://raw.github.com/gist/1436661/sfdc-bookmarklet-loader.js%23v=23.0&u=https://raw.github.com/gist/1436661/toggle-language.js')">Toggle Language (en &lt;=&gt; ja) </a>
</body>
</html>
/*global sforce:true */
(function() {
sforce.connection.getUserInfo(function(userInfo) {
var user = new sforce.SObject('User');
user.Id = userInfo.userId;
user.LanguageLocaleKey = /^ja/.test(userInfo.userLanguage) ? 'en_US' : 'ja';
sforce.connection.update([ user ], function() {
location.reload()
});
});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment