Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.3k stars 424 forks source link

[BUG] userscript sourcemap is mapping wrong variable #1621

Closed lisonge closed 2 years ago

lisonge commented 2 years ago

Describe the bug userscript sourcemap is mapping wrong variable

To Reproduce install it, then open https://i.songe.li/, press F12 and check console

// ==UserScript==
// @name               example
// @name:ja            hentai
// @name:zh            测试_
// @namespace          https://github.com/lisonge
// @version            1.0.1
// @author             lisonge
// @description        default description zh
// @description:zh     描述
// @description:en     description
// @description:ja     説明z
// @description:zh-CN  描述
// @license            MIT
// @icon               https://vitejs.dev/logo.svg
// @homepage           https://github.com/lisonge/vite-plugin-monkey#readme
// @homepageURL        https://github.com/lisonge/vite-plugin-monkey#readme
// @source             https://github.com/lisonge/vite-plugin-monkey.git
// @supportURL         https://github.com/lisonge/vite-plugin-monkey/issues
// @include            /^https:\/\/i\.songe\.li\/.*/
// @match              https://i.songe.li/
// @require            https://cdn.jsdelivr.net/npm/blueimp-md5@2.19.0
// @resource           element-plus/dist/index.css  https://cdn.jsdelivr.net/npm/element-plus@2.2.17/dist/index.css
// @grant              GM_cookie
// @grant              GM_getResourceText
// @grant              unsafeWindow
// ==/UserScript==

(e=>{const o=document.createElement("style");o.dataset.source="vite-plugin-monkey",o.innerText=e,document.head.appendChild(o)})(":root{font-family:Inter,Avenir,Helvetica,Arial,sans-serif;font-size:16px;line-height:24px;font-weight:400;color-scheme:light dark;color:#ffffffde;background-color:#242424;font-synthesis:none;text-rendering:optimizeLegibility;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;-webkit-text-size-adjust:100%}a{font-weight:500;color:#646cff;text-decoration:inherit}a:hover{color:#535bf2}body{margin:0;display:flex;place-items:center;min-width:320px;min-height:100vh}h1{font-size:3.2em;line-height:1.1}button{border-radius:8px;border:1px solid transparent;padding:.6em 1.2em;font-size:1em;font-weight:500;font-family:inherit;background-color:#1a1a1a;cursor:pointer;transition:border-color .25s}button:hover{border-color:#646cff}button:focus,button:focus-visible{outline:4px auto -webkit-focus-ring-color}.card{padding:2em}#app{max-width:1280px;margin:0 auto;padding:2rem;text-align:center}@media (prefers-color-scheme: light){:root{color:#213547;background-color:#fff}a:hover{color:#747bff}button{background-color:#f9f9f9}}");

(function(md52, client) {
  var _a;
  "use strict";
  const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
  const md5__default = /* @__PURE__ */ _interopDefaultLegacy(md52);
  const style = "";
  const cssLoader = (e) => {
    const t = GM_getResourceText(e), o = document.createElement("style");
    return o.innerText = t, document.head.append(o), t;
  };
  cssLoader("element-plus/dist/index.css");
  const v1 = 1;
  console.log("test");
  console.log({ v1 });
  console.log(`md5('114514')=${md5__default.default("114514")}`);
  console.log("document.readyState", document.readyState);
  console.log(client.monkeyWindow);
  if (client.unsafeWindow == window) {
    console.log("scope->host");
  } else {
    console.log("scope->monkey");
  }
  (_a = client.GM_cookie) == null ? void 0 : _a.list({}, (cookies, error) => {
    if (error) {
      console.log(error);
    } else {
      const [cookie] = cookies;
      if (cookie) {
        console.log(cookie);
      }
    }
  });
})(md5, (window.monkeyWindow = window, window));
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhhbXBsZS51c2VyLmpzIiwic291cmNlcyI6WyJDOi9Vc2Vycy9saXNvbmdlLy52c2NvZGUvdGVzdC50cyIsInNyYy9tYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBjb25zdCB2MSA9IDE7XHJcbmNvbnNvbGUubG9nKCd0ZXN0Jyk7XHJcbiIsImltcG9ydCAnLi9zdHlsZS5jc3MnO1xuLy8gaW1wb3J0IHBuZ1VybCBmcm9tICcuL2Fzc2V0L3Rlc3QucG5nJztcbi8vIGltcG9ydCBzdmdVcmwgZnJvbSAnLi9hc3NldC9mYXZpY29uLnN2Zyc7XG5pbXBv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QUFBQSxNQVlwQjtBQUFBLElBQ0Y7QUFBQSxFQUNGOzsiLCJzb3VyY2VSb290IjoiL2h0dHBzOi8vZ2l0aHViLmNvbS9saXNvbmdlL2V4YW1wbGUvIn0=

Expected behavior userscript sourcemap is mapping correct variable

Actual behavior userscript sourcemap is mapping wrong variable

image

Environment:


hello, I am developing dist.user.js sourcemap for vite-plugin-monkey

but I find Tampermonkey use wrapper function wrap userscript to run, It causes the code to offset N line backwards

and when use @require url, It also causes the code to offset N line backwards

so sourcemap will not map correct position

now I add sourcemap offset config for vite-plugin-monkey vite-plugin-monkey/src/node/types.ts#L308-L314 image

but the developer must know the number of lines of code offset in advance

and I find that the number of offset that Violentmonkey causes is different from the number of offset that Tampermonkey causes

This breaks the unity, It's very inconvenient

so I hope Tampermonkey can automatically modify the inline sourcemap of userscript

it is very simple to modify sourcemap if you just set offset line

let userscriptCode = `xxx`;
let sourceMappingURL: string = findSourceMap(userscriptCode);
const sourceMap = JSON.parse(await (await fetch(sourceMappingURL)).text());

const wrapperOffset: number = N1;
const requireOffset: number = N2;
const othersOffset: number = N3;

// your offset line
sourceMap.mappings =
  ';'.repeat(wrapperOffset + requireOffset + othersOffset) + sourceMap.mappings;

sourceMappingURL = toB64DataUrl(sourceMap);
userscriptCode = replaceSourceMap(userscriptCode, sourceMappingURL);

so are you willing to support automatically modify sourcemap?

lisonge commented 2 years ago

The reason for closing it

https://github.com/violentmonkey/violentmonkey/issues/1616

lisonge commented 2 years ago

@derjanb

can Tampermonkey stop adding the new line before // ==UserScript== ?

because it shifts line numbers for all scripts

image