Created
April 30, 2018 20:07
-
-
Save cuylerstuwe/1fc6bf55022a8e9e3d24b7b45b45e3d6 to your computer and use it in GitHub Desktop.
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
| // ==UserScript== | |
| // @name Troubleshoot Qualtrics Links in HITs | |
| // @namespace salembeats | |
| // @version 1.1 | |
| // @description . | |
| // @author Cuyler Stuwe (salembeats) | |
| // @include * | |
| // @grant none | |
| // @run-at document-start | |
| // ==/UserScript== | |
| if(window === window.top) {return;} | |
| if(!document.referrer.includes("worker.mturk.com/projects/")) {return;} | |
| function isQualtricsLink(link) { | |
| return ( link.innerText.toLowerCase().includes("qualtrics") || | |
| link.href.toLowerCase().includes("qualtrics") ); | |
| } | |
| function appendTroubleshootingButtonsToQualtricsLink(link) { | |
| let displayedURL; | |
| try { | |
| displayedURL = new URL(link.innerText.toLowerCase().trim().replace(/$www\./, "http://www.")); | |
| } catch(err) {} | |
| let hrefURL; | |
| try { | |
| hrefURL = new URL(link.href); | |
| } catch(err) {} | |
| let urlWithRepairedArugments; | |
| let isHrefURLMalformed = !Boolean(hrefURL); | |
| let isHrefAmpBeforeQuestion = Boolean( link.href.match(/&.*\?/) ); | |
| let isHrefAmpPresentWithoutQuestion = ( link.href.includes("&") && !link.href.includes("?") ); | |
| let isLinkedURLDifferentFromDisplayedURL = (Boolean(displayedURL) && Boolean(hrefURL) && (displayedURL.href.toLowerCase() !== hrefURL.href.toLowerCase())); | |
| let hasMalformedGETParameters = isHrefAmpBeforeQuestion || isHrefAmpPresentWithoutQuestion; | |
| let areIssuesDetected = isHrefURLMalformed || hasMalformedGETParameters || isLinkedURLDifferentFromDisplayedURL; | |
| if(hasMalformedGETParameters) { | |
| if(isHrefAmpPresentWithoutQuestion) { | |
| urlWithRepairedArguments = link.href.replace("&", "?"); | |
| } | |
| else if(isHrefAmpBeforeQuestion) { | |
| let ampIndex = link.href.indexOf("&"); | |
| let questionIndex = link.href.indexOf("?"); | |
| urlWithRepairedArguments[ampIndex] = "?"; | |
| urlWithRepairedArguments[questionIndex] = "&"; | |
| } | |
| else if(isHrefURLMalformed) { | |
| urlWithRepairedArguments = `javascript:void(0)`; | |
| } | |
| } | |
| link.insertAdjacentHTML("afterend", ` | |
| <style> | |
| .qualtrics-troubleshooting-container.problem { | |
| background: pink; | |
| } | |
| .qualtrics-troubleshooting-container.no-problem { | |
| background: palegreen; | |
| user-select: none; | |
| } | |
| </style> | |
| <span class="qualtrics-troubleshooting-container ${areIssuesDetected ? "problem" : "no-problem"}"> | |
| <span>Troubleshooting: </span> | |
| ${areIssuesDetected ? "" : `<span>Link appears to have no technical issues.</span>`} | |
| ${isLinkedURLDifferentFromDisplayedURL ? `<a class="qualtrics-troubleshooting-link" href="${displayedURL.href}">Try Displayed URL</a>` : ""} | |
| ${hasMalformedGETParameters ? `<a class="qualtrics-troubleshooting-link" href="${urlWithRepairedArguments}">Fix Malformed GET Parameters</a>` : ""} | |
| ${isHrefURLMalformed ? `<span>URL appears to be totally invalid and beyond simple repair. Please inspect manually.</span>` : ""} | |
| </span> | |
| `); | |
| } | |
| let qualtricsLinks; | |
| let frameObserver = new MutationObserver(mutations => { | |
| for(let mutation of mutations) { | |
| for(let addedNode of mutation.addedNodes) { | |
| if(addedNode.tagName && | |
| addedNode.tagName.toLowerCase() === "a" && | |
| isQualtricsLink(addedNode)) { | |
| if(!qualtricsLinks.includes(addedNode)) { | |
| appendTroubleshootingButtonsToQualtricsLink(addedNode); | |
| qualtricsLinks.push(addedNode); | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| function main() { | |
| let initialPageLinks = document.querySelectorAll("a"); | |
| qualtricsLinks = Array.from(initialPageLinks).filter( isQualtricsLink ); | |
| for(let qualtricsLink of qualtricsLinks) { | |
| appendTroubleshootingButtonsToQualtricsLink(qualtricsLink); | |
| } | |
| frameObserver.observe(document, {childList: true, subtree: true}); | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment