Categories
JavaScript Answers

How to make Chrome Extension run every page load with JavaScript?

Sometimes, we want to make Chrome Extension run every page load with JavaScript.

In this article, we’ll look at how to make Chrome Extension run every page load with JavaScript.

How to make Chrome Extension run every page load with JavaScript?

To make Chrome Extension run every page load with JavaScript, we call chrome.tabs.onUpdated.addListener with a callback to runs on every page load.

For instance, we write

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  if (changeInfo.status === "complete") {
    // ...
  }
});

to call chrome.tabs.onUpdated.addListener with a callback that runs on every page load.

If changeInfo.status is 'complete', then the page is loaded and we can do whatever we want after the page is loaded.

Conclusion

To make Chrome Extension run every page load with JavaScript, we call chrome.tabs.onUpdated.addListener with a callback to runs on every page load.

Categories
JavaScript Answers

How to convert a HTMLElement to a string with JavaScript?

Sometimes, we want to convert a HTMLElement to a string with JavaScript.

In this article, we’ll look at how to convert a HTMLElement to a string with JavaScript.

How to convert a HTMLElement to a string with JavaScript?

To convert a HTMLElement to a string with JavaScript, we can use the outerHTML property.

For instance, we write

const element = document.getElementById("new-element-1");
const elementHtml = element.outerHTML;

to get the element with getElementById.

Then we get the HTML code of the element with the outerHTML property.

Conclusion

To convert a HTMLElement to a string with JavaScript, we can use the outerHTML property.

Categories
JavaScript Answers

How to use two Y axes in Chart.js v2 and JavaScript?

Sometimes, we want to use two Y axes in Chart.js v2 and JavaScript.

In this article, we’ll look at how to use two Y axes in Chart.js v2 and JavaScript.

How to use two Y axes in Chart.js v2 and JavaScript?

To use two Y axes in Chart.js v2 and JavaScript, we can put the axes in the yAxes array property.

For instance, we write

const canvas = document.getElementById("chart");
new Chart(canvas, {
  type: "line",
  data: {
    labels: ["1", "2", "3", "4", "5"],
    datasets: [
      {
        label: "A",
        yAxisID: "A",
        data: [100, 50, 0],
      },
      {
        label: "B",
        yAxisID: "B",
        data: [1, 1, 1],
      },
    ],
  },
  options: {
    scales: {
      yAxes: [
        {
          id: "A",
          type: "linear",
          position: "left",
        },
        {
          id: "B",
          type: "linear",
          position: "right",
          ticks: {
            max: 1,
            min: 0,
          },
        },
      ],
    },
  },
});

to set yAxes to an array with the properties for each y-axis.

We set the type, position, and the min and max ticks values.

Conclusion

To use two Y axes in Chart.js v2 and JavaScript, we can put the axes in the yAxes array property.

Categories
JavaScript Answers

How to re-enable right click to let us inspect HTML elements in Chrome with JavaScript?

Sometimes, we want to re-enable right click to let us inspect HTML elements in Chrome with JavaScript.

In this article, we’ll look at how to re-enable right click to let us inspect HTML elements in Chrome with JavaScript.

How to re-enable right click to let us inspect HTML elements in Chrome with JavaScript?

To re-enable right click to let us inspect HTML elements in Chrome with JavaScript, we can set the oncontextmenu of elements in our page to null.

For instance, we can set the right click handler of the window to null by running

window.oncontextmenu = null;

in the console.

We can also run

const elements = document.getElementsByTagName("*");
for (const element of elements) {
  element.oncontextmenu = null;
}

to select all elements on the page with getElementByTagName.

Then we loop through each element in elements and set its oncontextmenu property to null.

Conclusion

To re-enable right click to let us inspect HTML elements in Chrome with JavaScript, we can set the oncontextmenu of elements in our page to null.

Categories
JavaScript Answers

How to copy output of a JavaScript variable to the clipboard?

Sometimes, we want to copy output of a JavaScript variable to the clipboard.

In this article, we’ll look at how to copy output of a JavaScript variable to the clipboard.

How to copy output of a JavaScript variable to the clipboard?

To copy output of a JavaScript variable to the clipboard, we can use the navigator.clipboard.writeText method.

For instance, we write

const copyToClipboard = (text) => {
  return navigator.clipboard.writeText(text);
};

to define the copyToClipboard function that takes the text string parameter.

In it, we call navigator.clipboard.writeText with text to copy the text string content into the clipboard.

Conclusion

To copy output of a JavaScript variable to the clipboard, we can use the navigator.clipboard.writeText method.