Skip to content

Commit

Permalink
Slash command improvments #450 and other
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoudkooi committed Dec 1, 2023
1 parent 80d00d4 commit abb51f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# CHANGELOG.md
## 7.1.7.0 (2023-12-01)
Fixes / changes:
- The random switch -r now works together with other switches and search text (Issue #450)
- Slash commands that search tables, but have no return fields defined now default to sys_updated_on,sys_updated_by so that you can use the right arrow to run query inline anyway.
- Slash commands that search tables, now show the total results when running inline query
- Fix typo innfo > info in inject.js
- Fix the /up UI Policy slash command (PR #454)

## 7.1.6.1 (2023-11-18 Chrome only)
Fixes / changes:
- Fix when User Icon does not show in Next Experience (Issue #453)
Expand Down
54 changes: 33 additions & 21 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ var snuslashcommands = {
"hint": "UnManadtory; Set all mandatory fields to false (Admin only)"
},
"up": {
"url": "sys_ui_policy_list.do?sysparm_query=nameLIKE$0^ORDERBYDESCsys_updated_on",
"url": "sys_ui_policy_list.do?sysparm_query=short_descriptionLIKE$0^ORDERBYDESCsys_updated_on",
"hint": "UI Policies <name>",
"fields": "name"
"fields": "short_description,sys_updated_on"
},
"va": {
"url": "/$conversation-builder.do",
Expand Down Expand Up @@ -349,7 +349,7 @@ var snuslashcommands = {
var snuslashswitches = {
"t": { "description": "View Table Structure ➚", "value": "sys_db_object.do?sys_id=$0&sysparm_refkey=name", "type": "link" },
"n": { "description": "New Record ➚", "value": "$0.do", "type": "link" },
"r": { "description": "Open Random Record ➚", "value": "$random.$0", "type": "link" },
"r": { "description": "Open Random Record ➚", "value": "&snurandom=true", "type": "querypart" },
"ra": { "description": "REST API Explorer ➚", "value": "$restapi.do?tableName=$0", "type": "link" },
"c": { "description": "Table Config ➚", "value": "personalize_all.do?sysparm_rules_table=$0&sysparm_rules_label=$0", "type": "link" },
"erd": { "description": "View Schema Map ➚", "value": "generic_hierarchy_erd.do?sysparm_attributes=table_history=,table=$0,show_internal=true,show_referenced=true,show_referenced_by=true,show_extended=true,show_extended_by=true,table_expansion=,spacing_x=60,spacing_y=90,nocontext", "type": "link" },
Expand Down Expand Up @@ -469,7 +469,7 @@ function snuGetDirectLinks(targeturl, shortcut) {
var fields = "";
var overwriteurl = "";
try {
fields = (snuslashcommands[shortcut].hasOwnProperty("fields")) ? snuslashcommands[shortcut].fields || "" : "";
fields = (snuslashcommands[shortcut].hasOwnProperty("fields")) ? snuslashcommands[shortcut].fields || "sys_updated_on,sys_updated_by" : "sys_updated_on,sys_updated_by";
} catch (e) { }

try {
Expand All @@ -488,7 +488,7 @@ function snuGetDirectLinks(targeturl, shortcut) {
} catch (ex) {
return false;
}
snuFetchData(g_ck, url, null, function (jsn) {
snuFetchData(g_ck, url, null, jsn => {
var directlinks = '';
if (jsn.hasOwnProperty('result')) {
var results = jsn.result;
Expand Down Expand Up @@ -525,6 +525,9 @@ function snuGetDirectLinks(targeturl, shortcut) {
}
directlinks += dispIdx + ' <a ' + idattr + '" target="' + target + '" href="' + link + '">' + txt + '</a><br />';
});
if (directlinks.length > 50)
directlinks += `<span style="opacity:0.4; font:smaller">Results: ${jsn.resultcount}</br><br />`;

}
else {
directlinks = `No access to data`;
Expand Down Expand Up @@ -754,14 +757,7 @@ function snuSlashCommandAddListener() {
query = query.trim();


targeturl = targeturl.replace(/\$0/g, query + (e.key.length == 1 ? e.key : ""));
if (query.split(" ").length > 0) { //replace $1,$2 for Xth word in string
var queryArr = query.split(" ");
for (var i = 0; i <= queryArr.length; i++) {
var re = new RegExp("\\$" + (i + 1), "g");
targeturl = targeturl.replace(re, queryArr[i] || "");
}
}
targeturl = snuResolve$(targeturl, query, e);
if (e.key == 'ArrowRight' || (e.key == 'Enter' && inlineOnly && !(e.ctrlKey || e.metaKey))) {
snuSlashLog(true);
snuGetDirectLinks(targeturl, shortcut);
Expand Down Expand Up @@ -993,7 +989,7 @@ function snuSlashCommandAddListener() {
if (typeof qry != 'undefined') {
if (targeturl.includes("{}") || linkSwitch) {
targeturl = targeturl.replace('{}', qry.getTableName());
if (!targeturl.startsWith("$random")) window.open(targeturl, '_blank');
if (!targeturl.includes("snurandom=true")) window.open(targeturl, '_blank');
}
else if (targeturl.startsWith("&")) {
var myurl = doc.location.href
Expand Down Expand Up @@ -1022,10 +1018,10 @@ function snuSlashCommandAddListener() {
else if (!linkSwitch){
targeturl = "/" + g_form.getTableName() + "_list.do?sysparm_query=" + targeturl;
}
if (!targeturl.startsWith("$random"))
if (!targeturl.includes("snurandom=true"))
window.open(targeturl, '_blank');
}
if (!targeturl.startsWith("$random")) {
if (!targeturl.includes("snurandom=true")) {
snuSlashCommandHide();
return;
}
Expand Down Expand Up @@ -1122,10 +1118,13 @@ function snuSlashCommandAddListener() {

var inIFrame = !targeturl.startsWith("http") && !targeturl.startsWith("/") && sameWindow;
if (e.target.className == "snutils") inIFrame = false;
if (targeturl.startsWith("$random")) {
if (targeturl.includes("snurandom=true")) {
let searchParams = new URLSearchParams(targeturl.split("?")[1]);
let q = searchParams.get("sysparm_query") || "";
let tableName = targeturl.split("_list.do")[0] || "notable";
targeturl = targeturl.substring(8)
snuGetRandomRecord(targeturl, "", false, res => {
targeturl = targeturl + ".do?sys_id=" + res;
snuGetRandomRecord(tableName, q, false, res => {
targeturl = tableName + ".do?sys_id=" + res;
if (inIFrame){
var nxtHdr = window.querySelectorShadowDom?.querySelectorDeep("sn-polaris-header");
if (nxtHdr){ //next experience
Expand Down Expand Up @@ -1397,6 +1396,18 @@ function snuDiffXml(shortcut, instance = '') {
}


function snuResolve$(targeturl, query, e) {
targeturl = targeturl.replace(/\$0/g, query + (e.key.length == 1 ? e.key : ""));
if (query.split(" ").length > 0) { //replace $1,$2 for Xth word in string
var queryArr = query.split(" ");
for (var i = 0; i <= queryArr.length; i++) {
var re = new RegExp("\\$" + (i + 1), "g");
targeturl = targeturl.replace(re, queryArr[i] || "");
}
}
return targeturl;
}

function snuResolveVariables(variableString){

let gsft = (document.querySelector("#gsft_main") || document.querySelector("[component-id]")?.shadowRoot.querySelector("#gsft_main"));
Expand Down Expand Up @@ -2513,7 +2524,7 @@ function snuAddTechnicalNames() {
try {
jQuery('h1.navbar-title div.pointerhand').css("float", "left");
jQuery("h1.navbar-title:not(:contains('|'))").first().append('<span class="snuwrap">&nbsp;| <span style="font-family:monospace; font-size:small;">' + g_form.getTableName() +
' <a onclick="snuShowScratchpad()">[scratchpad]</a><a title="Show the originating table innfo of fields" onclick="snuExtendedFieldInfo()">[table fields]</a>'+
' <a onclick="snuShowScratchpad()">[scratchpad]</a><a title="Show the originating table info of fields" onclick="snuExtendedFieldInfo()">[table fields]</a>'+
'<a title="For easier copying of field names" onclick="snuToggleLabel()">[toggle label]</a> </span></span>');

jQuery(".label-text:not(:contains('|'))").each(function (index, elem) {
Expand Down Expand Up @@ -3412,7 +3423,8 @@ async function snuFetchData(token, url, post, callback) {
headers,
body: post ? JSON.stringify(post) : null
});
const data = response.ok ? await response.json() : response;
let data = response.ok ? await response.json() : response;
data.resultcount = response.headers.get("X-Total-Count");
if (callback) callback(data);
resolve(data);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"short_name": "SN Utils",
"description": "Productivity tools for ServiceNow. (Personal work, not affiliated to ServiceNow)",
"author": "Arnoud Kooi / arnoudkooi.com",
"version": "7.1.6.1",
"version": "7.1.7.0",
"manifest_version": 3,
"permissions": [
"activeTab",
Expand Down

0 comments on commit abb51f8

Please sign in to comment.