Drasters원스
Drasters원스

Reputation: 1

Alert box on click link google doc

I am very new to all of this, and I just wanted to know if it's possible to have an alert box every time someone is clicking on a link in my google doc, I have the script so that an alert pops up every time you open the doc, but I want another one every time you click on a link, no specific, just all the links in the doc.

Upvotes: 0

Views: 711

Answers (1)

Tanaike
Tanaike

Reputation: 201388

I believe your goal is as follows.

  • When a text of hyperlink on Google Document is clicked, you want to open a dialog as an alert.
  • You want to achieve this using Google Apps Script.

Issue and workaround:

Unfortunately, in the current stage, OnSelectionChange and OnEdit triggers cannot be used for Google Document. So, it is required to use a workaround. At this time, I remembered my recent post. I thought that when this workaround is used, your goal might be able to be indirectly achieved. In this answer, I would like to propose a workaround.

This workaround uses Javascript. And, the pseudo OnEdit trigger is achieved using Javascript. So this workaround uses a sidebar.

Sample script:

Google Apps Script side: Code.gs

Please copy and paste this script to the script editor of Google Document as a script file.

function openDialog() {
  const doc = DocumentApp.getActiveDocument();
  const cursor = doc.getCursor();
  if (!cursor) return;
  const ps = cursor.getElement();
  if (ps.getType() != DocumentApp.ElementType.TEXT) return;
  const link = ps.asText().getLinkUrl();
  if (!link) return;
  const text = `<p>This is a text with a hyperlink.</p><p>The link is <a href="${link}" target="_blank">${link}</a></p>`;
  DocumentApp.getUi().showModalDialog(HtmlService.createHtmlOutput(text).setWidth(350).setHeight(100), "sample");
  Utilities.sleep(5000); // Please adjust this value for your actual situation.
}

// Please run this function.
function openSidebar() {
  DocumentApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile("index").setTitle("Sample"));
}

Javascript & HTML side: index.html

Please copy and paste this script to the script editor of Google Document as a HTML file.

<input type="button" onclick="start()" id="b1" value="start" />
<input type="button" onclick="stop()" id="b2" value="stop" disabled />
<script>
  let run;
  const runGoogleScript = (_) => new Promise((resolve, reject) => google.script.run.withFailureHandler(reject).withSuccessHandler(resolve).openDialog());
  const main = (_) => new Promise((resolve, reject) => setTimeout(async () => await runGoogleScript().then(resolve).catch(reject), 1000));

  const start = async () => {
    const b1 = document.getElementById("b1");
    const b2 = document.getElementById("b2");
    b1.disabled = true;
    b2.disabled = false;
    run = true;
    console.log("Start.");
    while (run) console.log(await main());
    console.log("End.");
    b1.disabled = false;
    b2.disabled = true;
  };

  const stop = (_) => (run = false);
</script>

Testing:

Please run openSidebar. By this, the sidebar is opened. And you can use this script by clicking "start" button. The demonstration movie can be seen in the following GIF image.

enter image description here

References:

Upvotes: 1

Related Questions