LoginSignup
ike81818
@ike81818

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Tampermonkeyでユーザースクリプトを外部ローカルファイルから読み込みたい

自己解決しました

Chrome(Brave)の右上三本線
→「拡張機能」
→ Tampermonkeyの「詳細」
→「ファイルのURLへのアクセスを許可する」のスイッチをONにする

以上の設定をすると正常に外部ファイルを読み込むようになりました

(参考)
https://scrapbox.io/yuugata/%E3%83%AD%E3%83%BC%E3%82%AB%E3%83%AB%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%A7%E4%BD%9C%E6%88%90%E3%81%97%E3%81%9FTampermonkey_script%E3%82%92%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%82%80

解決したいこと

Tampermonkeyでユーザースクリプトを外部ローカルファイルから読み込みたいです。
OSは、Windows10です。

Tampermonkyの編集画面に記載したユーザースクリプトコード

@requreのところで、外部ローカルファイルの読み込みを指定しましたが、読み込めません。

remove_ngword
// ==UserScript==
// @name         remove_ngword
// @version      0.1
// @description
// @author       ike
// @match        *://*/*
// @grant        none
// @require file:///c:/Users/ike/Desktop/jogai/output.js
// ==/UserScript==

読み込みたい外部ローカルファイルoutput.js

c:\Users\ike\Desktop\jogai\output.js
// ==UserScript==
// @name         WebAborn
// @namespace    http://tampermonkey.net/
// @version      19.20220729113717
// @description  Reduce the situation you see disagreeable texts in the way replacing to some word.  ('aborn' means 'purge and unable to read'.)
// @match     *://*/*
// ==/UserScript==
(function () {
  "use strict";
  const abornString = "";
  const ng_words = [
    "\u731b\u6691",
    "\u5e74\u91d1",
  ];
  let webaborn = function (node) {
    let candidates = document.evaluate(
      ".//text()[not(parent::style) and not(parent::textarea) and not(parent::script)]",
      node,
      null,
      6,
      null
    );
    let i, j, lenC, lenNG, txt;
    for (i = 0, lenC = candidates.snapshotLength; i < lenC; i++) {
      txt = candidates.snapshotItem(i).nodeValue;
      for (j = 0, lenNG = ng_words.length; j < lenNG; j++) {
        if (txt.indexOf(ng_words[j]) >= 0) {
          candidates.snapshotItem(i).nodeValue = abornString;
          break;
        }
      }
    }
    candidates = document.evaluate(
      './/input[not(@type="text")]/@value | .//img/@alt | .//*/@title | .//a/@href',
      node,
      null,
      6,
      null
    );
    for (i = 0, lenC = candidates.snapshotLength; i < lenC; i++) {
      txt = candidates.snapshotItem(i).value;
      for (j = 0; j < lenNG; j++) {
        if (txt.indexOf(ng_words[j]) >= 0) {
          candidates.snapshotItem(i).value = abornString;
          break;
        }
      }
    }
  };
  let nodeText = document.evaluate("//text()", document, null, 6, null);
  let nodePre = document.evaluate("//pre", document, null, 6, null);
  if (nodeText.snapshotLength === 1 && nodePre.snapshotLength === 1) {
    let del = nodeText.snapshotItem(0);
    let lines = del.nodeValue.split(/\r?\n/);
    let ins = document.createElement("pre");
    ins.style.whiteSpace = "pre-wrap";
    del.parentNode.replaceChild(ins, del);
    let i, len;
    for (i = 0, len = lines.length; i < len; i++) {
      ins.appendChild(document.createTextNode(lines[i]));
      ins.appendChild(document.createElement("br"));
    }
  }
  webaborn(document);
  document.addEventListener(
    "DOMNodeInserted",
    function (e) {
      webaborn(e.target);
    },
    false
  );
  document.addEventListener(
    "DOMCharacterDataModified",
    function (e) {
      webaborn(e.target);
    },
    false
  );
  document.addEventListener(
    "DOMAttrModified",
    function (e) {
      webaborn(e.target);
    },
    false
  );
})();

設定

スクリーンショット 2022-09-25 230238.png

参考にしたサイト

教えていただきたいこと

上記ユーザースクリプトremove_ngwordの@requireのあとの、パスの書き方を教えていただきたいです。

0

1Answer

読み込めない原因はパスの書き方ではなくて設定では?

設定>【全般】設定のモード「上級者」>【セキュリティ】スクリプトによるローカル ファイルへのアクセスを許可する

0

Comments

  1. @ike81818

    Questioner
    ご回答ありがとうございます。
    Tampermonkeyの設定を調べたところ、上記スクリーンショットのようになっていて、
    「スクリプトによるローカルファイルへのアクセスを許可する」
    という記述がありませんでした。

Your answer might help someone💌